ReDim array w/ variables

franklin_m

New Member
Joined
Jun 16, 2013
Messages
47
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I'm continuing my effort to learn to use arrays. What I'd like to do is create one array, determine it's size, and then create a second array and dimension (or redimension) to a size that is a transpose of the first. In other words, if Array1 is 10 rows and 2 columns, I'd read UBound rows & columns of Array1 and create a second array that is 2 rows by 10 columns. I'm doing this so I don't screw up the math on complex data transformation that's running slowly due to size. I want to do the calcs inside arrays to speed things up.

So here's the code I thought would work, but when I run it againts a simple 12 row by 2 column set of data, it gives "Script out of range" and debugger hangs on line beginning "MonthValuesTX(j, i) ... "

I'm pretty sure it's something simple, but for the life of me I can't see it. Thanks.

VBA Code:
Sub ArrayTest_v1()

Dim MonthValues() As Variant
MonthValues = Worksheets("ArraySandbox").Range("A2:B13").Value

Dim RowsInArray As Long
Dim ColsInArray As Long
RowsInArray = UBound(MonthValues, 1)
ColsInArray = UBound(MonthValues, 2)

Dim MonthValuesTX() As Variant
ReDim MonthValues(ColsInArray, RowsInArray)

Dim i As Long
Dim j As Long
For i = 1 To RowsInArray
    For j = 1 To ColsInArray
        MonthValuesTX(j, i) = MonthValues(i, j)
    Next j
Next i

End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Try
VBA Code:
ReDim MonthValuesTX(1 to ColsInArray, 1 to RowsInArray)
 
Upvote 0
You used ReDim with MonthValues not MonthValuesTX.
 
Upvote 0
Solution

Forum statistics

Threads
1,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

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