Fill the worksheet using a Jagged Array faster

ssmock1

New Member
Joined
Sep 13, 2013
Messages
1
I know I can quickly fill a worksheet with an array referencing a value using something like this:
Dim thisArray(6 To 139, 6 To 40) As Variant
Dim x As String
dim row, col As Integer

for row from 6 to 139 step 1
for col from 6 to 40 step 1
thisArray(row, col) = row * col
next col
next row

x = "F6:AN139"
ActiveWorkbook.Sheets(1).Range(x) = thisArray

however, if "thisArray" references a one-dimensional array with two elements,

thisArray(6, 6) = Array(amt, errCode)

this type of assignment won't work. I'd like to select the first element in the referenced array to fill the range. I tried this but the compiler complained about the wrong number of dimensions.

Activeworkbook.Sheets(1).Range(x) = thisArray(0)

I've got it to work by going cell by cell and making the correct assignment, but it's slow compared to the direct assignment method referenced in the first example.
 
Last edited:

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
I know I can quickly fill a worksheet with an array referencing a value using something like this:
Dim thisArray(6 To 139, 6 To 40) As Variant
Dim x As String
dim row, col As Integer

for row from 6 to 139 step 1
for col from 6 to 40 step 1
thisArray(row, col) = row * col
next col
next row

x = "F6:AN139"
ActiveWorkbook.Sheets(1).Range(x) = thisArray

however, if "thisArray" references a one-dimensional array with two elements,

thisArray(6, 6) = Array(amt, errCode)

this type of assignment won't work. I'd like to select the first element in the referenced array to fill the range. I tried this but the compiler complained about the wrong number of dimensions.

Activeworkbook.Sheets(1).Range(x) = thisArray(0)

I've got it to work by going cell by cell and making the correct assignment, but it's slow compared to the direct assignment method referenced in the first example.
If it is a one-dimensional, just make it a two dimensional array anyway and then you will be able to use the fast first example method (of course you will have to remember to tag on that second dimension when referencing the array in your code). I am thinking of something like this...

Rich (BB code):
Dim thisArray(6 To 139, 1 To 1) As Variant
Dim x As String
dim row As Long
For row = 6 To 139
  thisArray(row, 1) = row * row
Next
x = "F6:F139"
ActiveWorkbook.Sheets(1).Range(x) = thisArray

Note1: The red "1 To 1" in the Dim statement creates the second dimension that will not change, the red 1 inside the loop is the "place-holder" for that second dimension.

Note2: The green "Long" in the Dim statement is better to use than Integer... in today's computers, there is nothing to be gained by restricting your variable to type Integer.
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top