Read in data from an excel spreadsheet?


Hi,

I am using a vbScript and I need to be able to read in 4 columns of data from an excel file, and place these values into a dynamic array of some sort.

There are 4 columns of data in the excel sheet.  The columns will be a different length everytime the code is ran, but the columns will be the same length as each other.

So after the code has executed I need to have the data from the 4 columns in an array (or 4 different arrays??), in the same order as it was in the excel spreadsheet.

Cany anyone provide this code?
Many Thanks
Steven
sterankinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

R_RajeshCommented:

FirstColumn = 1 '1=A,2=B...
Set xlWkb = CreateObject("Excel.Application").workbooks.open("C:\SampleBook.xls")
With xlWkb.sheets(1)
myArray = .Range(.cells(FirstColumn, 1), _
.cells(.Range("D65536").end(&HFFFFEFBE).row, FirstColumn + 3))
End With
xlWkb.Close False

For i = LBound(myArray) To UBound(myArray)
For j = 1 To 4
str1 = str1 & myArray(i, j) & vbTab
Next j
str1 = str1 & vbCrLf
Next i
MsgBox str1
0
R_RajeshCommented:
Slight modification. D65536 was hardcoded...

FirstColumn = 1 '1=A,2=B...
Set xlWkb = CreateObject("Excel.Application").workbooks.open("C:\SampleBook.xls")
With xlWkb.sheets(1)
myArray = .Range(.cells(FirstColumn, 1), _
.cells(.cells(65536, FirstColumn).end(&HFFFFEFBE).row, FirstColumn + 3))
End With
xlWkb.Close False

For i = LBound(myArray) To UBound(myArray)
For j = 1 To 4
str1 = str1 & myArray(i, j) & vbTab
Next j
str1 = str1 & vbCrLf
Next i
MsgBox str1
0
sterankinAuthor Commented:

Thanks - quick query before I resolve this - is there anyway I can ignore the first cells, that is ignore A1, B1, C1 and D1?
As these are the column headers.

Thanks for the help.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

R_RajeshCommented:
replace
myArray = .Range(.cells(FirstColumn, 1), _
.cells(.cells(65536, FirstColumn).end(&HFFFFEFBE).row, FirstColumn + 3))
with
myArray = .Range(.cells(FirstColumn, 2), _
.cells(.cells(65536, FirstColumn).end(&HFFFFEFBE).row, FirstColumn + 3))

second argument in cells(FirstColumn, 2) determines which row the range starts from...
0
sterankinAuthor Commented:
Thanks - I did try that but I got a subscirpt out of range error at thsi line:

str1 = str1 & myArray(i, j) & vbTab

any ideas?
0
R_RajeshCommented:
Sorry, had row and column mixed up...

myArray = .Range(.cells(2, FirstColumn), _
.cells(.cells(65536, FirstColumn).end(&HFFFFEFBE).row, FirstColumn + 3))
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sterankinAuthor Commented:
Thanks for the help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.