Filling dynamic arrays vba

rharn

Board Regular
Joined
Jun 21, 2011
Messages
54
I am trying to fill a dynamic array with the row indexes from a search function. So far I have created a loop that introduces a leading or trailing 0 to fill the array. I would like to fill the array without the 0 but my current code keeps giving me a 'subscript out of range' error. This should be a easy fix, I just dont understand VBA enough to fix it myself. All help appreciated!

Code:
    Dim Results1() As Variant
    Dim TextBox1 As Long
    Dim FindRange1 As Range
    Dim Find1 As Range
    Dim i1 As Long

    ReDim Results1(1)
    TextBox1 = ILsearch.TextBox1.Value
 
                If ILsearch.P1B1.Value = True Then
                    For Each Find1 In FindRange1
                        If (Find1.Value < TextBox1) And (Find1.Value > 0) Then
                            Results1(i1) = Find1.Row
                            i1 = i1 + 1
                            ReDim Preserve Results1(i1)
                        End If
                    Next Find1
                End If
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Then that's why it's failing. You haven't set the value of il before using it, so it's 0 at that point.
If Option Base 1 is required for something else you can explicitly
redim Results(0 to 0) but within your loop, you must also
Redim preserve Results(0 to il)

Or of course, just initialise il to 1 before using it for the first time, then leave everything else as it was.
Even simpler, remove Option Base 1 if it's not required.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,876
Members
449,056
Latest member
ruhulaminappu

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