[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Reading an Excel file into VB variables

Posted on 2011-10-13
7
Medium Priority
?
314 Views
Last Modified: 2012-06-22
(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.
0
Comment
Question by:NormaPosy
  • 4
  • 3
7 Comments
 
LVL 17

Expert Comment

by:Daniel Reynolds
ID: 36965644
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.
0
 
LVL 17

Expert Comment

by:Daniel Reynolds
ID: 36965648
This link on the site may be of some use to you.
It has a coding example in it.

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_10217411.html
0
 

Author Comment

by:NormaPosy
ID: 36966442
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")
oWS.Select

'Select cell A1
oWS.Range("A1").Select

'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
oXL.Quit
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"
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Accepted Solution

by:
Daniel Reynolds earned 2000 total points
ID: 36966520
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.
0
 

Author Comment

by:NormaPosy
ID: 36968594
How do I do that?
0
 
LVL 17

Expert Comment

by:Daniel Reynolds
ID: 36969528
0
 

Author Closing Comment

by:NormaPosy
ID: 36971596
Thank you
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question