Solved

VB program get data directly from Excel spreadsheet

Posted on 2004-08-02
10
203 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
Independent Software Vendors: 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 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

Technology Partners: 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!

Question has a verified solution.

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

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…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

710 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