Append to array without knowing ahead how many to add to

30percent

Board Regular
Joined
May 5, 2011
Messages
123
Office Version
  1. 2016
Platform
  1. Windows
Hi,

how do I append an item to an array without knowing how many to add to ahead of time? I created a generic example to illustrate.

Code:
Public Sub Add2Arr()


Set sht = ThisWorkbook.Worksheets("Sheet1")
lastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row


Dim maxArr() As Variant


For i = 1 To lastRow


    If sht.Range("A" & i).Value Mod 2 = 0 Then
        'append to maxArr() array then find the max of even numbers
    Else
       'append to maxArr() array then find the max of odd numbers
    End If
    
Next


End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
30percent,

The following demonstrates the use of ReDim Preserve to add an item to an array...

Code:
Public Sub Add2Arr_1023455r2()
Dim sht As Worksheet
Dim lastrow As Long, i As Long, n As Long
Dim maxArr() As Variant

Set sht = ThisWorkbook.Worksheets("Sheet1")
lastrow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
n = 1
For i = 1 To lastrow
    If sht.Range("A" & i).Value Mod 2 = 0 Then
        ReDim Preserve maxArr(1 To 1, 1 To n)
        maxArr(1, n) = sht.Range("A" & i).Value
        n = n + 1
    End If
Next
MsgBox "max even " & WorksheetFunction.Max(Application.Index(maxArr, , n - 1))
End Sub

Given the generic example, you could also consider using "lastrow" to set the upper bound for the array...

Code:
Public Sub Add2Arr_1023455()
Dim sht As Worksheet
Dim lastrow As Long, i As Long
Dim maxArr() As Variant

Set sht = ThisWorkbook.Worksheets("Sheet1")
lastrow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
ReDim maxArr(1 To lastrow, 1 To 2)
For i = 1 To lastrow
    If sht.Range("A" & i).Value Mod 2 = 0 Then
        maxArr(i, 1) = sht.Range("A" & i).Value
    Else
       maxArr(i, 2) = sht.Range("A" & i).Value
    End If
Next
MsgBox "max even " & WorksheetFunction.Max(Application.Index(maxArr, , 1))
MsgBox "max odd " & WorksheetFunction.Max(Application.Index(maxArr, , 2))
End Sub

Cheers,

tonyyy
 
Upvote 0
Hi Tonyyy,

I did the following to look at the structure of the array of odd numbers:

Code:
For i = LBound(Application.Index(maxArr, , 2)) To UBound(Application.Index(maxArr, , 2))
    Debug.Print "row " & i & " value is " & maxArr(i, 2)
Next

it resulted in:
row 1 value is 1
row 2 value is
row 3 value is 3
row 4 value is
row 5 value is 5
row 6 value is
row 7 value is 7
row 8 value is
row 9 value is 9
row 10 value is

So there are 10 rows for 5 items.
How do we code it so that, it would be 5 rows for 5 items. Same for the even column.

such as
row 1 value is 1
row 2 value is 3
row 3 value is 5
row 4 value is 7
row 5 value is 9

Thank you!
 
Upvote 0
Use the construct in the first macro...

Code:
Public Sub Add2Arr_1023455r2()
Dim sht As Worksheet
Dim lastrow As Long, i As Long, n As Long
Dim maxArr() As Variant

Set sht = ThisWorkbook.Worksheets("Sheet1")
lastrow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
n = 1
For i = 1 To lastrow
    If sht.Range("A" & i).Value Mod 2 = 0 Then
        ReDim Preserve maxArr(1 To 1, 1 To n)
        maxArr(1, n) = sht.Range("A" & i).Value
        [COLOR=#ff0000]Debug.Print maxArr(1, n)[/COLOR]
        n = n + 1
    End If
Next
MsgBox "max even " & WorksheetFunction.Max(Application.Index(maxArr, , n - 1))
End Sub

Using ReDim Preserve in a loop and incrementing the index (n = n + 1) will resize the array to fit exactly the number of even (or odd) numbers.

The use of ReDim Preserve in a loop - while accomplishing what you want - isn't very efficient, thus the second macro which sizes the array only once. Having blank items in the array doesn't matter if all you're trying to do is find the max value... but maybe your example wasn't really representative of what you're trying to do.
 
Upvote 0
Hi,

This is what I coded. However, the max of the array didn't come out as expected.

Code:
Public Sub bucketMinMax()
Set sht = ThisWorkbook.Worksheets("Sheet1")


finalRow = sht.Cells(sht.Rows.Count, "a").End(xlUp).Row


Dim maxArr50() As Variant
Dim n As Long


n = 1


For i = 2 To finalRow
    Bucket = sht.Range("N" & i).Value
    Select Case Bucket
        Case 50
            ReDim Preserve maxArr50(1 To 1, 1 To n)
            maxArr50(1, n) = sht.Range("S" & i).Value
            n = n + 1
            
    End Select
Next




For i = LBound(maxArr50, 2) To UBound(maxArr50, 2)
    Debug.Print maxArr50(1, i)
Next


Max = WorksheetFunction.Max(Application.Index(maxArr50, 1))
Debug.Print "Max is " & Max


End Sub

The outcome of the debug.print is:

81
54
95
Max is 81

The max should be 95. I messed around with this line of code to see if I could find out why the max is 81 instead of 95 but I couldn't figure out.

Max = WorksheetFunction.Max(Application.Index(maxArr50, 1))
 
Upvote 0
Application.Index(maxArr50, 1) will return the first value in maxArr50.
 
Upvote 0
Not sure what you want your function to do; it seems a very inefficient way to find that value. However, the last line you need is:

Code:
Max = WorksheetFunction.Max(maxArr50)

WBD
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

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