Passing range address to array

rharn

Board Regular
Joined
Jun 21, 2011
Messages
54
Hi all,

I am trying to write a procedure that will insert the address of a cell within a range into an array if it meets a requirement. What I have so far is:

Code:
                        For R1 = 1 To Max
                            If (Find1.Value < TextBox1) And (Find1.Value > "0") Then
                                Results1(R1) = Find1.Address
                                R1 = R1 + 1
                            End If
                        Next R1

Where Find1.Value is a value within a range and R1 is the counter for the array Results1(). However, this keeps displaying a subscript out of range error. I think this is a very simple error but I am relatively new to VBA so much help would be appreciated!

Thanks!
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
The subscript out of range error can mean that the index (R1) is outside the bounds of the array (Results1). Example array has bounds of 1 to 10 and Max = 12. At some point in you code prior to this for loop you should have the two lines of code something like this:
Code:
Dim Results1() As String
ReDim Results1(1 to Max)

subscript out of range error can occur for other reasons but I am guess this is were the error occurred hence your posting it.

Below is just your code rewritten to express the string values as numbers as it appears you are making numeric comparison. Not strictly needed but I think it is better practice.

Code:
For R1 = 1 To Max
    If (Find1.Value < CDbl(TextBox1.Text)) And (Find1.Value > 0) Then
        Results1(R1) = Find1.Address
        R1 = R1 + 1
    End If
Next R1
 
Upvote 0
Why incrementing R1 if it's incremented automatically?
This style of coding is error-prone.
 
Upvote 0
Hi Rob,

Thanks for the tip. Since I am creating a dynamic array, is there a way to size the array as the loop progresses? Currently my Results1() array is sized somewhat arbitrarily to the same size as the data range to ensure that it can fit all data. But I feel it might be more efficient if I can size the size of the results array so it does not have any blank rows in the array.

Thanks in advance!
 
Upvote 0
Code:
[COLOR="Blue"]Sub[/COLOR] Arrs()
    
    [COLOR="Blue"]Dim[/COLOR] Max [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Long[/COLOR], R1 [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Long[/COLOR], i [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Long[/COLOR], Results1() [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Variant[/COLOR]
    
    Max = 100
    
    [COLOR="Blue"]For[/COLOR] R1 = 1 [COLOR="Blue"]To[/COLOR] Max
        [COLOR="Blue"]If[/COLOR] (Find1.Value < TextBox1) [COLOR="Blue"]And[/COLOR] (Find1.Value > "0") [COLOR="Blue"]Then[/COLOR]
            i = i + 1
            [COLOR="Blue"]ReDim[/COLOR] [COLOR="Blue"]Preserve[/COLOR] Results1(i)
            Results1(i) = Find1.Address
        [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
    [COLOR="Blue"]Next[/COLOR] R1
    
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,514
Messages
6,179,225
Members
452,896
Latest member
IGT

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