I'm Back. LOL
I've modified the code to collect data from all 8 cells with the goal to create the .xlsx file first of the month then write the data to a different tab each day. I'm writing the date as tab name (yyyy mm dd) and headers for the columns I'm using. However when the date rolls over it creates the new tab with the correct date but doesn't write the data to it. I've tried various ways to mess around with this "ExcelPackage.Workbook.Worksheets(1)" but no luck. Can you point me in the right direction???
Also is it possible to get a confirmation bit back to PLC that data was written??
The code and .xlsx file attached.
Thanks.
Private Sub DataSubscriber1_DataChanged(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles DataSubscriber1.DataChanged
If e.ErrorId = 0 AndAlso e.Values.Count > 0 AndAlso e.Values(0) = "True" Then
Console.WriteLine("About to read the data")
Dim MyValues0() As String = EthernetIPforCLXCom1.Read("VCell1B_FES_Cycle_Average[0]", 500)
Dim MyValues1() As String = EthernetIPforCLXCom1.Read("VCell_1A_FES_Cycle_Average[0]", 500)
Dim MyValues2() As String = EthernetIPforCLXCom1.Read("VCell_2B_FES_Cycle_Average[0]", 500)
Dim MyValues3() As String = EthernetIPforCLXCom1.Read("VCell_2A_FES_Cycle_Average[0]", 500)
Dim MyValues4() As String = EthernetIPforCLXCom1.Read("VCell_3B_FES_Cycle_Average[0]", 500)
Dim MyValues5() As String = EthernetIPforCLXCom1.Read("VCell_3A_FES_Cycle_Average[0]", 500)
Dim MyValues6() As String = EthernetIPforCLXCom1.Read("Inspect_FES_Cycle_Average[0]", 500)
Dim MyValues7() As String = EthernetIPforCLXCom1.Read("Repair_FES_Cycle_Average[0]", 500)
Dim MyValues8 As String = EthernetIPforCLXCom1.Read("MDY5")
Console.WriteLine(MyValues0.Length & "elements read VCell_1B.")
Console.WriteLine(MyValues1.Length & "elements read VCell_1A.")
Console.WriteLine(MyValues2.Length & "elements read VCell_2B.")
Console.WriteLine(MyValues3.Length & "elements read VCell_2A.")
Console.WriteLine(MyValues4.Length & "elements read VCell_3B.")
Console.WriteLine(MyValues5.Length & "elements read VCell_3A.")
Console.WriteLine(MyValues6.Length & "elements read Inspect.")
Console.WriteLine(MyValues7.Length & "elements read Repair.")
Console.WriteLine("All Data Read")
'*Transfer the values to Excel
'*Creates Excel file
Using ExcelPackage As New OfficeOpenXml.ExcelPackage(New IO.FileInfo("C:\Data.xlsx"))
ExcelPackage.Workbook.Worksheets.Add(MyValues8)
Console.WriteLine("ws creation ok")
'*Writes the Headers in Row 1
ExcelPackage.Workbook.Worksheets(1).Cells("A1").Value = "VCell 1B"
ExcelPackage.Workbook.Worksheets(1).Cells("C1").Value = "VCell 1A"
ExcelPackage.Workbook.Worksheets(1).Cells("E1").Value = "VCell 2B"
ExcelPackage.Workbook.Worksheets(1).Cells("G1").Value = "VCell 2A"
ExcelPackage.Workbook.Worksheets(1).Cells("I1").Value = "VCell 3B"
ExcelPackage.Workbook.Worksheets(1).Cells("K1").Value = "VCell 3A"
ExcelPackage.Workbook.Worksheets(1).Cells("M1").Value = "Inspect"
ExcelPackage.Workbook.Worksheets(1).Cells("O1").Value = "Repair"
Console.WriteLine("header creation ok")
'*Loops to write data in correct columns
For Index = 0 To MyValues0.Length - 1
Console.WriteLine("Element " & Index & "=" & MyValues0(Index))
Dim ws As OfficeOpenXml.ExcelWorksheet = ExcelPackage.Workbook.Worksheets(1)
Dim CellNum As String = "A" & (Index + 3)
ws.Cells(CellNum).Value = MyValues0(Index)
Next
Console.WriteLine("Worksheet OK 1B")
For Index = 0 To MyValues1.Length - 1
Console.WriteLine("Element " & Index & "=" & MyValues1(Index))
Dim ws As OfficeOpenXml.ExcelWorksheet = ExcelPackage.Workbook.Worksheets(1)
Dim CellNum As String = "C" & (Index + 3)
ws.Cells(CellNum).Value = MyValues1(Index)
Next
Console.WriteLine("Worksheet OK 1A")
For Index = 0 To MyValues2.Length - 1
Console.WriteLine("Element " & Index & "=" & MyValues2(Index))
Dim ws As OfficeOpenXml.ExcelWorksheet = ExcelPackage.Workbook.Worksheets(1)
Dim CellNum As String = "E" & (Index + 3)
ws.Cells(CellNum).Value = MyValues2(Index)
Next
Console.WriteLine("Worksheet OK 2B")
For Index = 0 To MyValues3.Length - 1
Console.WriteLine("Element " & Index & "=" & MyValues3(Index))
Dim ws As OfficeOpenXml.ExcelWorksheet = ExcelPackage.Workbook.Worksheets(1)
Dim CellNum As String = "G" & (Index + 3)
ws.Cells(CellNum).Value = MyValues3(Index)
Next
Console.WriteLine("Worksheet OK 2A")
For Index = 0 To MyValues4.Length - 1
Console.WriteLine("Element " & Index & "=" & MyValues4(Index))
Dim ws As OfficeOpenXml.ExcelWorksheet = ExcelPackage.Workbook.Worksheets(1)
Dim CellNum As String = "I" & (Index + 3)
ws.Cells(CellNum).Value = MyValues4(Index)
Next
Console.WriteLine("Worksheet OK 3B")
For Index = 0 To MyValues5.Length - 1
Console.WriteLine("Element " & Index & "=" & MyValues5(Index))
Dim ws As OfficeOpenXml.ExcelWorksheet = ExcelPackage.Workbook.Worksheets(1)
Dim CellNum As String = "K" & (Index + 3)
ws.Cells(CellNum).Value = MyValues5(Index)
Next
Console.WriteLine("Worksheet OK 3A")
For Index = 0 To MyValues6.Length - 1
Console.WriteLine("Element " & Index & "=" & MyValues6(Index))
Dim ws As OfficeOpenXml.ExcelWorksheet = ExcelPackage.Workbook.Worksheets(1)
Dim CellNum As String = "M" & (Index + 3)
ws.Cells(CellNum).Value = MyValues6(Index)
Next
Console.WriteLine("Worksheet OK Inspect")
For Index = 0 To MyValues7.Length - 1
Console.WriteLine("Element " & Index & "=" & MyValues7(Index))
Dim ws As OfficeOpenXml.ExcelWorksheet = ExcelPackage.Workbook.Worksheets(1)
Dim CellNum As String = "O" & (Index + 3)
ws.Cells(CellNum).Value = MyValues7(Index)
Next
Console.WriteLine("Worksheet OK Repair")
'*Saves.xlsl file
ExcelPackage.Save()
End Using
End If
End Sub