Solved

VB program get data directly from Excel spreadsheet

Posted on 2004-08-02
10
195 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now