How Populating Arrays/Range in VBA code

R

robinson.william

Hey All,

I have a set of 500 or so numbers that i want to use for debugging.
These numbers are in matrix form (30 rows) x (18 cols) and i need to
hard code these numbers into cells for debugging. I would like to
insert these values into a range of the same size ( Range("B2:S31")) is
there a easy way of doing this?

sorta like Range("B2:S31") = [1 , 2, 3, 4......]

i've tried the same thing with populating a multidimensional array and
the only way i can do it is to write one line of code per element of
the array.Can anybody help me with this?
 
A

Andy Pope

Hi,

This demonstrates how to read in a range into a variant array in 1 go.
Loop through the elements in the array and final output the value again.

Sub Test()

Dim vntArray As Variant
Dim lngRow As Long
Dim lngCol As Long

vntArray = Range("B2:S31")

For lngRow = LBound(vntArray, 1) To UBound(vntArray, 1)
For lngCol = LBound(vntArray, 2) To UBound(vntArray, 2)
Debug.Print vntArray(lngRow, lngCol)
vntArray(lngRow, lngCol) = _
vntArray(lngRow, lngCol) + (lngCol * lngRow)
Next
Next

' uncomment to output changed values
' Range("B2:S31") = vntArray

End Sub

Cheers
Andy
 
R

robinson.william

Thanks Andy,
but this isn't my problem, my problem comes from taking the raw data
(like a CSV file) and entering it into an array/range

Andy said:
Hi,

This demonstrates how to read in a range into a variant array in 1 go.
Loop through the elements in the array and final output the value again.

Sub Test()

Dim vntArray As Variant
Dim lngRow As Long
Dim lngCol As Long

vntArray = Range("B2:S31")

For lngRow = LBound(vntArray, 1) To UBound(vntArray, 1)
For lngCol = LBound(vntArray, 2) To UBound(vntArray, 2)
Debug.Print vntArray(lngRow, lngCol)
vntArray(lngRow, lngCol) = _
vntArray(lngRow, lngCol) + (lngCol * lngRow)
Next
Next

' uncomment to output changed values
' Range("B2:S31") = vntArray

End Sub

Cheers
Andy

Hey All,

I have a set of 500 or so numbers that i want to use for debugging.
These numbers are in matrix form (30 rows) x (18 cols) and i need to
hard code these numbers into cells for debugging. I would like to
insert these values into a range of the same size ( Range("B2:S31")) is
there a easy way of doing this?

sorta like Range("B2:S31") = [1 , 2, 3, 4......]

i've tried the same thing with populating a multidimensional array and
the only way i can do it is to write one line of code per element of
the array.Can anybody help me with this?
 
A

Andy Pope

I'm a little confused, which is not difficult ;)
If you have a csv file can you not open it in excel, which will in
effect put the values in to a matrix. Then read the range in to a array?

Cheers
Andy

Thanks Andy,
but this isn't my problem, my problem comes from taking the raw data
(like a CSV file) and entering it into an array/range

Andy said:
Hi,

This demonstrates how to read in a range into a variant array in 1 go.
Loop through the elements in the array and final output the value again.

Sub Test()

Dim vntArray As Variant
Dim lngRow As Long
Dim lngCol As Long

vntArray = Range("B2:S31")

For lngRow = LBound(vntArray, 1) To UBound(vntArray, 1)
For lngCol = LBound(vntArray, 2) To UBound(vntArray, 2)
Debug.Print vntArray(lngRow, lngCol)
vntArray(lngRow, lngCol) = _
vntArray(lngRow, lngCol) + (lngCol * lngRow)
Next
Next

' uncomment to output changed values
' Range("B2:S31") = vntArray

End Sub

Cheers
Andy

Hey All,

I have a set of 500 or so numbers that i want to use for debugging.
These numbers are in matrix form (30 rows) x (18 cols) and i need to
hard code these numbers into cells for debugging. I would like to
insert these values into a range of the same size ( Range("B2:S31")) is
there a easy way of doing this?

sorta like Range("B2:S31") = [1 , 2, 3, 4......]

i've tried the same thing with populating a multidimensional array and
the only way i can do it is to write one line of code per element of
the array.Can anybody help me with this?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top