Reading an Excel file into VB variables

(VB6) The usual:

Open <file> For Input As #1
Line Input #1, <string>
Close #1

Works for .txt files.

How does one open a .xls file and read from it into a VB variable?

I suppose cell-by-cell, but I would expect to be able read an entire row, perhaps into a declared type.

I've never done this.
Norma PosyPhysicistAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Daniel ReynoldsSoftware Applications Developer / IntegratorCommented:
That method will not work as an excel file is not a flat-file.
YOu are in VB6 and it has been awhile since I tried that in VB6.

In the .Net framework, you include the namespaces for the MSOffice and are able to instantiate and open an excel file and read thru it either row by row or cell by cell. YOu can also read a particular cell address.

It will be helpful for you to open excel and create some macros reading thru the spreadsheet that you want to load. This give you an idea of how to reference and walk thru a spreadsheet.
Daniel ReynoldsSoftware Applications Developer / IntegratorCommented:
This link on the site may be of some use to you.
It has a coding example in it.
Norma PosyPhysicistAuthor Commented:
Private Sub cmdGo_Click()
Dim oXL As Object ' Excel.Application
Dim oWB As Object ' Excel.Workbook
Dim oWS As Object ' Excel.Worksheet
Dim sPathName As String
Dim lngRow As Long
Dim iI As Integer

'Start Excel
Set oXL = New Excel.Application

'Open a workbook, select a sheet, assign variables to them
sPathName = App.Path & "\F2011_League_Roster.xls"
Set oWB = oXL.Workbooks.Open(sPathName)
Set oWS = oWB.Sheets("Division")

'Select cell A1

'Show a messagebox before Excel shows up
MsgBox "When Excel shows, please select an entire row of data"

'Show Excel
oXL.Visible = True

'Loop until an entire row has been selected
Do:   Loop Until (oXL.Selection.Columns.Count = 256) _
And (oXL.Selection.Rows.Count = 1)

'Hide Excel again
oXL.Visible = False

'Store row number of selected row
lngRow = oXL.Selection.Row

'Retrieve values from the selected row to textboxes
For iI = 1 To 14
    txtInput(iI - 1).Text = oWS.Cells(lngRow, iI).Value
Next iI
'Close workbook and shut down Excel
oWB.Close SaveChanges:=False
Set oXL = Nothing
Set oWB = Nothing
Set oWS = Nothing

End Sub

Open in new window

Bombs at:
Set oXL = New Excel.Application
"User defined type not defined"
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Daniel ReynoldsSoftware Applications Developer / IntegratorCommented:
Don't forget....For this to work, you must set a reference to the Microsoft Excel Object Library.
Otherwise you get the User Defined type not defined.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Norma PosyPhysicistAuthor Commented:
How do I do that?
Daniel ReynoldsSoftware Applications Developer / IntegratorCommented:
Norma PosyPhysicistAuthor Commented:
Thank you
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.