Message 1 of 7
Read Excel
Not applicable
07-29-2006
05:14 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I have some code which should read an excel file. When I use the buttons my Listbox remains empty. I have loaded the Excel Object Library. What is wrong?
Private Sub CommandButton1_Click()
Dim excelApp As Excel.Application
Dim wbkObj As Workbook
Dim shtObj As WorkSheet
On Error Resume Next
UserForm1.Hide
Err.Clear
Set excelApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Err.Clear
Set excelApp = CreateObject("Excel.Application")
If Err <> 0 Then
MsgBox "Cannot start Excel", vbExclamation
End
End If
End If
excelApp.Visible = True
Set wbkObj = excelApp.Workbooks.Open("C:\Bob\Test.xls")
Set shtObj = excelApp.Worksheets(1)
UserForm1.Show
End Sub
Private Sub CommandButton2_Click()
Dim excelApp As Excel.Application
Dim wbkObj As Workbook
Dim shtObj As WorkSheet
Dim i As Integer
Dim txt As String
On Error Resume Next
UserForm1.Hide
Err.Clear
Set excelApp = GetObject(, "Excel.Appication")
excelApp.Visible = True
Set shtObj = excelApp.Worksheets(1)
ListBox1.Clear
i = 1
txt = shtObj.Cells(i, 2)
Do While txt <> ""
ListBox1.AddItem txt
i = i + 1
txt = shtObj.Cells(i, 2)
Loop
UserForm1.Show
End Sub
Private Sub CommandButton1_Click()
Dim excelApp As Excel.Application
Dim wbkObj As Workbook
Dim shtObj As WorkSheet
On Error Resume Next
UserForm1.Hide
Err.Clear
Set excelApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Err.Clear
Set excelApp = CreateObject("Excel.Application")
If Err <> 0 Then
MsgBox "Cannot start Excel", vbExclamation
End
End If
End If
excelApp.Visible = True
Set wbkObj = excelApp.Workbooks.Open("C:\Bob\Test.xls")
Set shtObj = excelApp.Worksheets(1)
UserForm1.Show
End Sub
Private Sub CommandButton2_Click()
Dim excelApp As Excel.Application
Dim wbkObj As Workbook
Dim shtObj As WorkSheet
Dim i As Integer
Dim txt As String
On Error Resume Next
UserForm1.Hide
Err.Clear
Set excelApp = GetObject(, "Excel.Appication")
excelApp.Visible = True
Set shtObj = excelApp.Worksheets(1)
ListBox1.Clear
i = 1
txt = shtObj.Cells(i, 2)
Do While txt <> ""
ListBox1.AddItem txt
i = i + 1
txt = shtObj.Cells(i, 2)
Loop
UserForm1.Show
End Sub