?
Solved

VB program get data directly from Excel spreadsheet

Posted on 2004-08-02
10
Medium Priority
?
206 Views
Last Modified: 2010-05-02
How can I get VB to get data directly from an Excel spreadsheet?  Right now, I'm converting the Excel file to CSV, then making it a text file, then using INPUT AS to get it into the VB program.

There's gotta be a way to do this directly, with no conversions needed, but how?  (Note I'm using VB 5.0 Pro)
0
Comment
Question by:kayton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 8

Expert Comment

by:dds110
ID: 11696824
You can use ole objects to open the workbook and then you can reference the cells directly.
Or.

You can access excel just like you would a database.  here's an article on that:
http://support.microsoft.com/default.aspx?kbid=190195

I'll try to find some reference material for you on the ole objects.  Till then, look up automation in your help files.


HTH
0
 
LVL 8

Expert Comment

by:dds110
ID: 11697049
I'm back.

Here's an example of late binding.  This function gets the data located in A1 of Sheet1

Function getXLData()
Dim xl, wb, ws
Dim theResult As String

Set xl = CreateObject("EXCEL.Application")
Set wb = xl.Workbooks.Open("C:\Your\Path\Your\Workbook.xls")
Set ws = wb.Worksheets("Sheet1")

With ws
    .Activate
    theResult = Range("A1")
End With

wb.Close
xl.Quit

Set xl = Nothing

MsgBox theResult
End Function


HTH
0
 
LVL 2

Author Comment

by:kayton
ID: 11697203
The function inputs one value.  I need to input many columns and rows worth of data, and store the data into arrays.  For instance column 1 becomes the array LastName(i) and column 2 becomes FirstName(i).  How do you implement this?  I'm new to VB, and there's nothing like this in older versions of Basic to help me.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Author Comment

by:kayton
ID: 11699923
dds-
When I try the code you sent, I get an error message since VB thinks range("A1") is an array and it's not dimensioned.  How should this code read?
0
 
LVL 8

Expert Comment

by:dds110
ID: 11857138
Hey,

Sorry I've been away so long.  If you havn't got it figured out yet, paste your code and I'll see what I can do.


DDS
0
 
LVL 2

Author Comment

by:kayton
ID: 11857221
It's still not working.  I'm trying to read in a bunch of cells into an array in VB.  What's wrong???  Thanks.

Private Sub Form_Load()
Call getXLData
Show.Form
End Sub

Function getXLData()
Dim xl, wb, ws
Dim LastName(4), junk As String

Set xl = CreateObject("EXCEL.Application")
Set wb = xl.Workbooks.Open("C:\Basic\labels.xls")
Set ws = wb.Worksheets("Sheet1")

With ws
    .Activate
    For i = 1 To 2
        junk = "a" + Str(i)
        LastName(i) = Range("junk")
        Print "lastname(i)="; LastName(i)
    Next i
End With

wb.Close
xl.Quit

Set xl = Nothing

MsgBox LastName(1)
MsgBox LastName(2)
End Function
0
 
LVL 8

Expert Comment

by:dds110
ID: 11928063
two things:

change this line:
junk = "a" + Str(i)

to this:

junk = "a" + Trim(Str(i))

then change this line:
LastName(i) = Range("junk")
to this:
LastName(i) = Range(junk)

That should do it.
0
 
LVL 2

Author Comment

by:kayton
ID: 11928131
I get an error message for Range(junk).  VB complains that Range is a sub or function that is not defined.  Revised code appears below.  What should I fix now???

*******
Private Sub Form_Load()
Call getXLData
Rem Show.Form
End Sub

Function getXLData()
Dim xl, wb, ws
Dim LastName(4), junk As String

Set xl = CreateObject("EXCEL.Application")
Set wb = xl.Workbooks.Open("C:\Basic\labels.xls")
Set ws = wb.Worksheets("Sheet1")

With ws
    .Activate
    For i = 1 To 2
        junk = "a" + Trim(Str(i))
        LastName(i) = Range(junk)
        Print "lastname(i)="; LastName(i)
    Next i
End With

wb.Close
xl.Quit

Set xl = Nothing

MsgBox LastName(1)
MsgBox LastName(2)
End Function
0
 
LVL 8

Accepted Solution

by:
dds110 earned 200 total points
ID: 12070370
Sorry again for being away so long.  This one is simple.  Since you are using late binding to reference Excel, you need to precede any of Excels functions (i.e. Range) with your reference to Excel.  In this case, just change the line to read:

LastName(i) = xl.Range(junk)

Hope that helps.
0
 
LVL 2

Author Comment

by:kayton
ID: 12098619
I'm on vacation now - will try this when I get back in a few days.  Thanks!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

718 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