I am trying to get the results from a listbox into an existing Excel file. I have googled extensively, but I don't seem to be able to come up with anything from a .NET basis. I will show you what I have (primitive and incomplete). Please give me a direction to follow.
Dim xlApp As New Excel.Application
Dim xlWkBook = xlApp.Workbooks.Open("C:\NFL_2012.xls")
Dim xlWkSheet = xlWkBook.Worksheets("Sheet_1")
xlWkSheet.Range("A1").Value = ListBox10.Items.Item(0)
xlWkSheet.Range("B1").Value = ListBox10.Items.Item(1)
Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Dec 4, 2012, 8:47 PM EST
Private Sub ReportToExcel_Click()
'EXPORT to EXCEL
Dim i As Long 'variable for ColumnCount
Dim j As Long 'variable for ListCount
myExApp.Visible = True 'Sets Excel visible
Dim myExApp As New Excel.Application
Dim xlWkBook = myExApp.Workbooks.Open("C:\NFL_2012.xls")
Dim myExSheet= xlWkBook.Worksheets("Sheet_1")
For i = 1 To ReportRecordList.ColumnCount 'Counter for ColumnCount
ReportRecordList.BoundColumn = ReportRecordList.BoundColumn + 1 'Setting counter for BoundColumn
For j = 1 To ReportRecordList.ListCount 'Counter for ListCount
myExSheet.Cells(j , i) = ReportRecordList.ItemData(j - 1) 'Insert ItemData into Excel Worksheet
Next j 'Iterating through ListCount
Next i 'Iterating through ColumnCount
ReportRecordList.BoundColumn = 1 'Setting BoundColumn to original 1
Set myExSheet = Nothing 'Release Worksheet
Set myExApp = Nothing 'Release Excel Application
End Sub
Duane68 reputationPosted Dec 5, 2012, 7:26 AM EST
Dim xlWkSheet ... is where my debugger always locks up with the usual "0x8..." type of code that is nearly indecipherable. I can submit my error code, but I doubt if it will be of any use. I am obviously not coding that particular line correctly. Also, how can you use a variable before you have declared it (myExApp.Visible?).
Duane68 reputationPosted Dec 10, 2012, 5:09 PM EST
http://www.vb-helper.com/howto_net_write_excel.html