brianmac59
New member
- Joined
- Nov 10, 2024
- Messages
- 4
- Programming Experience
- 3-5
I am using VBnet(VS2022), i have a program that add lines to a csv file. Everything works fine. I now need to modify it to an excel file. I have that working. My problem is, the second time the program is run it writes the new lines in the right row, but deleted the previous lines and blanks them out. Here is the code, I know I have a ton of extra lines, thats just because I have been trying everything to make it work...
VB.NET:
Dim path As String = "d:\PickPackAudit.xlsx"
' Build Excel for testing ---------------------------------------------------------------------------------- START
Dim xlApp As Excel.Application = New Microsoft.Office.Interop.Excel.Application()
xlApp.DisplayAlerts = False
Dim xlWorkBook As Excel.Workbook = xlApp.Workbooks.Add()
Dim worksheets As Excel.Sheets = xlWorkBook.Worksheets
Dim xlNewSheet = DirectCast(worksheets.Add(worksheets(1), Type.Missing, Type.Missing, Type.Missing), Excel.Worksheet)
Dim Vardate As Date
Dim Vartime As Date
Dim VRtot As Decimal
Dim x As Integer = 0
Dim u As Integer = 0
Dim z As Integer = 0 'row of excel to write to
Dim A As Integer = 0
Dim VRcheck As String
Dim nextrow As Integer
x = DataGridView1.RowCount
Vartime = System.DateTime.Now
xlNewSheet.Name = "Pick Audit"
Do Until u = x
If DataGridView1(6, u).Value > "" Then
VRtot = DataGridView1(9, u).Value * DataGridView1(7, u).Value
If File.Exists(path) Then
If A = 0 Then
z = xlNewSheet.UsedRange.Rows.Count + 2
A = 1
End If
'Write Detail
xlNewSheet.Cells(z, 1) = Vartime
xlNewSheet.Cells(z, 2) = (DataGridView1(0, u).Value)
xlNewSheet.Cells(z, 3) = (DataGridView1(1, u).Value)
xlNewSheet.Cells(z, 4) = (DataGridView1(2, u).Value)
xlNewSheet.Cells(z, 5) = (DataGridView1(3, u).Value)
xlNewSheet.Cells(z, 6) = (DataGridView1(4, u).Value)
xlNewSheet.Cells(z, 7) = (DataGridView1(5, u).Value)
xlNewSheet.Cells(z, 8) = (DataGridView1(6, u).Value)
xlNewSheet.Cells(z, 9) = (DataGridView1(7, u).Value)
xlNewSheet.Cells(z, 10) = VRtot
xlNewSheet.Cells(z, 11) = (DataGridView1(9, u).Value)
xlWorkBook.SaveAs(path)
z = z + 1
Else
xlNewSheet.Name = "Pick Audit"
'Write Heading
z = 1
xlNewSheet.Cells(z, 1) = "Date"
xlNewSheet.Cells(z, 2) = "SO#"
xlNewSheet.Cells(z, 3) = "SO Line#"
xlNewSheet.Cells(z, 4) = "Part"
xlNewSheet.Cells(z, 5) = "Desc."
xlNewSheet.Cells(z, 6) = "Location"
xlNewSheet.Cells(z, 7) = "Bin"
xlNewSheet.Cells(z, 8) = "Remaining"
xlNewSheet.Cells(z, 9) = "Price"
xlNewSheet.Cells(z, 10) = "Amt"
xlNewSheet.Cells(z, 11) = "Phy Picked"
z = z + 1
'Write Detail
xlNewSheet.Cells(z, 1) = Vartime
xlNewSheet.Cells(z, 2) = (DataGridView1(0, u).Value)
xlNewSheet.Cells(z, 3) = (DataGridView1(1, u).Value)
xlNewSheet.Cells(z, 4) = (DataGridView1(2, u).Value)
xlNewSheet.Cells(z, 5) = (DataGridView1(3, u).Value)
xlNewSheet.Cells(z, 6) = (DataGridView1(4, u).Value)
xlNewSheet.Cells(z, 7) = (DataGridView1(5, u).Value)
xlNewSheet.Cells(z, 8) = (DataGridView1(6, u).Value)
xlNewSheet.Cells(z, 9) = (DataGridView1(7, u).Value)
xlNewSheet.Cells(z, 10) = VRtot
xlNewSheet.Cells(z, 11) = (DataGridView1(9, u).Value)
'xlWorkBook.SaveAs(path)
End Using
End If
End If
u = u + 1
Loop
End If
End If
u = u + 1
Loop