Solved

VB program get data directly from Excel spreadsheet

Posted on 2004-08-02
10
201 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
  • 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 50 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel VBA combine two working workbooks 8 60
Validating VB6 Function 19 65
MsgBox 2 59
clicking a shape in a frame array vb6 3 46
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…

809 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