Click to See Complete Forum and Search --> : can i enter data in excel thru vb?


coolily
January 20th, 2000, 10:10 AM
can i enter rows of data/ add spreadsheets to my excel file thru vb?

Lothar Haensler
January 20th, 2000, 10:21 AM
you can use OLE automation to do that.
Add a reference to the Excel Typelibrary to your vb project and adopt the following sample (passes listview data to excel)

private Sub Command1_Click()
Dim i as ListItem
Dim j as Integer
for j = 1 to 5
set i = l.ListItems.Add
i.Text = "col1"
i.SubItems(1) = "col2"
i.SubItems(2) = "col3"
next j
Dim x as Excel.Application
set x = CreateObject("Excel.Application")
Dim w as Excel.Workbook
set w = x.Workbooks.Add
x.Visible = true
Dim row
Dim col
Dim strRange as string
for row = 1 to l.ListItems.Count
strRange = "A" & row
w.ActiveSheet.Range(strRange).Value = l.ListItems(row).Text
for col = 1 to l.ColumnHeaders.Count - 1
strRange = Chr(Asc("A") + col) & row
Debug.print strRange
w.ActiveSheet.Range(strRange).Value = l.ListItems(row).SubItems(col)
next
next
End Sub