Search code examples
vbafor-loop

VBA:: Exiting loops


I am creating a search function that loops through a 1 dimensional array looking for answers specified by the user and my problem currently is that I only want the search to loop through the searched range once. However, it is currently stuck in the loop. Is there any suggestions on how I can exit this loop after it searches through the function once? I have currently the following code below. I am storing the cell addresses in the Results1() array and I only want each found term to be stored once

P1B1 is a radio button that specifies the function to search through the data range for any values less than the specified (Textbox1) value. FindRange1 is the range of data values I want the function to search through.

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

            Set FindRange1 = Worksheets("Properties").Range("P7:P1000")
            If ILsearch.P1B1.Value = True Then
                For i1 = 0 To Max
                    For Each Find1 In FindRange1
                        If (Find1.Value < TextBox1) And (Find1.Value > 0) Then
                            i1 = i1 + 1
                            ReDim Preserve Results1(i1)
                            Results1(i1) = Find1.Address
                        End If
                    Next Find1
                Next i1
            End If

Solution

  • Just get rid for the For i1=0 to Max loop - judging by your requirements it is not needed, as you only seem to want to visit every element in FindRange1 once.