Solved

VB program get data directly from Excel spreadsheet

Posted on 2004-08-02
10
193 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 2

Author Comment

by:kayton
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I'm on vacation now - will try this when I get back in a few days.  Thanks!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…

772 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