Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - populating two dimensional array with excel data

Reply
 
Old 07-18-2008   #1 (permalink)
Mecha77


 
 

populating two dimensional array with excel data

Hi,
I'm looking for an efficient way to populate a two dimensional array. The
first cell of each row contains the First Name and the second cell has the
Last Name. I'm ok up to the part where I actually need to read the data in

The code I have for reading the data into the array works but it is just
awful:

'loop reading each spreadsheet element into the array
n = 0 'counter for array element position
intRow = 1
intCol = 1


Do Until objExcel.Cells(intRow,intCol).Value = ""

Fullname(n,0) = objExcel.Cells(intRow, intCol).Value
intRow = intRow + 1
n = n + 1

Loop

intRow = 1
intCol = 2
n = 0

Do Until objExcel.Cells(intRow,intCol).Value = ""

Fullname(n,1) = objExcel.Cells(intRow, intCol).Value
intRow = intRow + 1
n = n + 1

Loop

There has to be a better way. Can someone show me?

Thanks in Advance


My System SpecsSystem Spec
Old 07-18-2008   #2 (permalink)
Old Pedant


 
 

RE: populating two dimensional array with excel data

For row = 1 to 999999
If objExcel.Cells(row, 1).Value = "" Then Exit For
Fullname(row-1,0) = objExcel.Cells( row, 1).Value
Fullname(row-1,1) = objExcel.Cells( row, 2).Value
Next

I should point out, though, that VBS typically works better with the columnd
and rows reversed from what you are using. The reason for that is that you
can do
ReDim Preserve arrayName( existingNumberOfColumns, newNumberOfRows )
to easily add rows to an array. With ReDim Preserve, only the *LAST*
dimension can be changed in the ReDim.

If this is strictly for your own use and you KNOW you won't need to expand
that FullName array, it won't matter, but consider it for the future.

My System SpecsSystem Spec
Old 07-18-2008   #3 (permalink)
Tim Williams


 
 

Re: populating two dimensional array with excel data

If there are only 2 columns of data on the sheet and it's a contiguous
block:

Dim arrData
arrData = objExcel.Cells(1,1).CurrentRegion.Value

arrData will be a 2-D array.

Tim.


"Mecha77" <Mecha77@xxxxxx> wrote in message
news:1A97F690-57AC-42D8-911B-BE5C79ADF61F@xxxxxx
Quote:

> Hi,
> I'm looking for an efficient way to populate a two dimensional array. The
> first cell of each row contains the First Name and the second cell has the
> Last Name. I'm ok up to the part where I actually need to read the data in
>
> The code I have for reading the data into the array works but it is just
> awful:
>
> 'loop reading each spreadsheet element into the array
> n = 0 'counter for array element position
> intRow = 1
> intCol = 1
>
>
> Do Until objExcel.Cells(intRow,intCol).Value = ""
>
> Fullname(n,0) = objExcel.Cells(intRow, intCol).Value
> intRow = intRow + 1
> n = n + 1
>
> Loop
>
> intRow = 1
> intCol = 2
> n = 0
>
> Do Until objExcel.Cells(intRow,intCol).Value = ""
>
> Fullname(n,1) = objExcel.Cells(intRow, intCol).Value
> intRow = intRow + 1
> n = n + 1
>
> Loop
>
> There has to be a better way. Can someone show me?
>
> Thanks in Advance
>

My System SpecsSystem Spec
Old 07-24-2008   #4 (permalink)
Mecha77


 
 

Re: populating two dimensional array with excel data

Thank you to Old Pedant and Tim for your help

"Tim Williams" wrote:
Quote:

> If there are only 2 columns of data on the sheet and it's a contiguous
> block:
>
> Dim arrData
> arrData = objExcel.Cells(1,1).CurrentRegion.Value
>
> arrData will be a 2-D array.
>
> Tim.
>
>
> "Mecha77" <Mecha77@xxxxxx> wrote in message
> news:1A97F690-57AC-42D8-911B-BE5C79ADF61F@xxxxxx
Quote:

> > Hi,
> > I'm looking for an efficient way to populate a two dimensional array. The
> > first cell of each row contains the First Name and the second cell has the
> > Last Name. I'm ok up to the part where I actually need to read the data in
> >
> > The code I have for reading the data into the array works but it is just
> > awful:
> >
> > 'loop reading each spreadsheet element into the array
> > n = 0 'counter for array element position
> > intRow = 1
> > intCol = 1
> >
> >
> > Do Until objExcel.Cells(intRow,intCol).Value = ""
> >
> > Fullname(n,0) = objExcel.Cells(intRow, intCol).Value
> > intRow = intRow + 1
> > n = n + 1
> >
> > Loop
> >
> > intRow = 1
> > intCol = 2
> > n = 0
> >
> > Do Until objExcel.Cells(intRow,intCol).Value = ""
> >
> > Fullname(n,1) = objExcel.Cells(intRow, intCol).Value
> > intRow = intRow + 1
> > n = n + 1
> >
> > Loop
> >
> > There has to be a better way. Can someone show me?
> >
> > Thanks in Advance
> >
>
>
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
how can i determine the excel rows that contain data PowerShell
Re: data into excel VB Script
Re: data into excel VB Script
excel data Vista General
2 dimensional array of objects VB Script


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46