How to redim multidimensional correctly

ouvay

Board Regular
Joined
Jun 9, 2022
Messages
131
Office Version
  1. 2019
Platform
  1. Windows
Hello

My code here (posting only error area) gives me subscription out of range error

VBA Code:
For i = LBound(l) To UBound(l)
    If l(i, 6) = "Completed" Then
        n = n + 1
        ReDim Preserve a(1 To n) As Variant
        a(n) = l(i, 1)
    End If
Next i

ReDim Preserve a(1 To UBound(a), 1 To 50) As Variant

I know we cannot redim the first dimensions of an array, that is why I wrote it like this.. but unfortunately I cannot expand my array columns

any advice?
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
You can't use Redim Preserve to increase the number of dimensions, e.g. from 1 to 2.

Why don't you just make it big enough in the first place?

VBA Code:
Dim a() As Variant

'...

ReDim a(1 To UBound(L) - LBound(L) + 1, 1 To 50)

For i = LBound(L) To UBound(L)
    If L(i, 6) = "Completed" Then
        n = n + 1
        a(n, 1) = L(i, 1)
    End If
Next i
 
Upvote 0
You can't use Redim Preserve to increase the number of dimensions, e.g. from 1 to 2.

Why don't you just make it big enough in the first place?

VBA Code:
Dim a() As Variant

'...

ReDim a(1 To UBound(L) - LBound(L) + 1, 1 To 50)

For i = LBound(L) To UBound(L)
    If L(i, 6) = "Completed" Then
        n = n + 1
        a(n, 1) = L(i, 1)
    End If
Next i
Thanks for the quick reply! I was trying to avoid doing that lol wanted to know if there was a more elegant solution which had evaded me but I guess the way arrays work leaves us with few options😅
Thanks so much coming through and helping!
 
Upvote 0
I know we cannot redim the first dimensions of an array, that is why I wrote it like this.. but unfortunately I cannot expand my array columns

any advice?

Actually you can redim both with the help of a UDF. It is past my bedtime, but I will see if I can locate it when I awaken if you want.
 
Upvote 0
Actually you can redim both with the help of a UDF. It is past my bedtime, but I will see if I can locate it when I awaken if you want.
that sounds amazing! no rush though.. get your solid 7 first :)
 
Upvote 0
Found it:

VBA Code:
Public Function ReDimPreserve(ArrayNameToPreserve, NewRowUbound, NewColumnUbound)
'
' Code inspired by Control Freak
'
' Redim & preserve both dimensions for a 2D array
'
' example usage of the function:
' ArrayName = ReDimPreserve(ArrayName,NewRowSize,NewColumnSize)
' ie.
' InputArray = ReDimPreserve(InputArray,10,20)
'
    ReDimPreserve = False
'
    If IsArray(ArrayNameToPreserve) Then                                                                    ' If the variable is an array then ...
        ReDim NewArrayNameToPreserve(NewRowUbound, NewColumnUbound)                                         '   Create New 2D Array
        OldRowUbound = UBound(ArrayNameToPreserve, 1)                                                       '   Save row Ubound of original array
        OldColumnUbound = UBound(ArrayNameToPreserve, 2)                                                    '   Save column Ubound of original array
'
        For NewRow = LBound(ArrayNameToPreserve, 1) To NewRowUbound                                         '   Loop through rows of original array
            For NewColumn = LBound(ArrayNameToPreserve, 2) To NewColumnUbound                               '       Loop through columns of original array
                If OldRowUbound >= NewRow And OldColumnUbound >= NewColumn Then
                    NewArrayNameToPreserve(NewRow, NewColumn) = ArrayNameToPreserve(NewRow, NewColumn)      '               Append additional rows/columns to NewArrayNameToPreserve
                End If
            Next                                                                                            '       Loop back
        Next                                                                                                '   Loop back
'
        If IsArray(NewArrayNameToPreserve) Then ReDimPreserve = NewArrayNameToPreserve
    End If
End Function
 
Upvote 0
Found it:

VBA Code:
Public Function ReDimPreserve(ArrayNameToPreserve, NewRowUbound, NewColumnUbound)
'
' Code inspired by Control Freak
'
' Redim & preserve both dimensions for a 2D array
'
' example usage of the function:
' ArrayName = ReDimPreserve(ArrayName,NewRowSize,NewColumnSize)
' ie.
' InputArray = ReDimPreserve(InputArray,10,20)
'
    ReDimPreserve = False
'
    If IsArray(ArrayNameToPreserve) Then                                                                    ' If the variable is an array then ...
        ReDim NewArrayNameToPreserve(NewRowUbound, NewColumnUbound)                                         '   Create New 2D Array
        OldRowUbound = UBound(ArrayNameToPreserve, 1)                                                       '   Save row Ubound of original array
        OldColumnUbound = UBound(ArrayNameToPreserve, 2)                                                    '   Save column Ubound of original array
'
        For NewRow = LBound(ArrayNameToPreserve, 1) To NewRowUbound                                         '   Loop through rows of original array
            For NewColumn = LBound(ArrayNameToPreserve, 2) To NewColumnUbound                               '       Loop through columns of original array
                If OldRowUbound >= NewRow And OldColumnUbound >= NewColumn Then
                    NewArrayNameToPreserve(NewRow, NewColumn) = ArrayNameToPreserve(NewRow, NewColumn)      '               Append additional rows/columns to NewArrayNameToPreserve
                End If
            Next                                                                                            '       Loop back
        Next                                                                                                '   Loop back
'
        If IsArray(NewArrayNameToPreserve) Then ReDimPreserve = NewArrayNameToPreserve
    End If
End Function
Thanks so much! I'm going to give this a go and get back to you
 
Upvote 0

Forum statistics

Threads
1,215,013
Messages
6,122,690
Members
449,092
Latest member
snoom82

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