Redim Preserve error r/t 9 subscpt out of range

jim may

Well-known Member
Joined
Jul 4, 2004
Messages
7,486
Code is bombing on code line in RED - what rule am I violating?

Rich (BB code):
Option Base 1
Sub Foo()
Dim arr1()
Dim arr2()
Dim Rng As Range
Dim FDate As Date
LR = Range("A" & Rows.Count).End(xlUp).Row
Set Rng = Range(Cells(2, 1), Cells(LR, 2))
arr1 = Rng
ReDim Preserve arr2(1, 2)
arr2(1, 1) = Cells(2, 1)
arr2(1, 2) = Cells(2, 2)
NumMonths = 1
FDate = arr1(1, 1)
    For i = 3 To LR
        If Month(Cells(i, 1)) <> Month(FDate) Then
        NumMonths = NumMonths + 1
        ReDim Preserve arr2(UBound(arr2, 1) + 1, 2)
        arr2(NumMonths, 1) = Cells(i, 1)
        arr2(NumMonths, 2) = Cells(i, 2)
        FDate = DateSerial(Year(FDate), Month(FDate) + 1, 1)
        End If
    Next i
    MsgBox arr2(1, 1) & " and " & arr2(1, 2)
    MsgBox arr2(2, 1) & " and " & arr2(2, 2)
    MsgBox arr2(3, 1) & " and " & arr2(3, 2)
    MsgBox arr2(4, 1) & " and " & arr2(4, 2)
End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
When using Redim Preserve you can only change the last dimension.
 
Upvote 0
When using Redim Preserve you can only change the last dimension.

Thanks Norie;
Can you spoon feed me a bit more here.. by "last dimension" do you mean the second parameter, the Column #?. In my case, what change should be made in order for the code to continue running?

TIA,

Jim :)
 
Upvote 0
Jim

In this array:
Rich (BB code):
ReDim Preserve arr2(UBound(arr2, 1) + 1, 2)

the last dimension is the 2, as far as I can see in your code you are using it to refer to columns.

If it was my code I'd just reverse the dimensions.

Doing that doesn't look like it'd cause a major problem to the purpose of the code, but doing that will be a hassle since the array is used quite a bit.

Every time I find myself having to do it I always seem to get it wrong somewhere and either end up with run-time errors or strange results.:eek:
 
Upvote 0

Forum statistics

Threads
1,224,505
Messages
6,179,151
Members
452,891
Latest member
JUSTOUTOFMYREACH

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