VBA Range.Find in multiple ranges... easier method?

svendiamond

Well-known Member
Joined
Jun 13, 2014
Messages
1,504
Office Version
  1. 365
Platform
  1. Windows
I am trying to find a string (myLookup) in range "balRangeA"

If I don't find it there, I want to look in "balRangeB"

If I don't find it there, I want to look in "incRangeA"

Is there an easier / shorter way to manage this rather than this code I currently have:

Code:
    Dim foundIt As Range
    Set foundIt = balRangeA.Find(myLookup, , , xlWhole)
    
    If foundIt Is Nothing Then
        Set foundIt = balRangeB.Find(myLookup, , , xlWhole)
    End If
    
    If foundIt Is Nothing Then
        Set foundIt = incRangeA.Find(myLookup, , , xlWhole)
    End If

I especially want to know this because I have a lot more ranges that I need to lookup and I don't want to keep writing this IF statement.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Try...

Code:
    Dim myRanges As Variant
    Dim myRange As Variant
    Dim foundIt As Range
    
    myRanges = Array(balRangeA, balRangeB, incRangeA)
    
    For Each myRange In myRanges
        Set foundIt = myRange.Find(myLookup, , , xlWhole)
        If Not foundIt Is Nothing Then Exit For
    Next myRange
    
    If Not foundIt Is Nothing Then
        MsgBox "The lookup value '" & myLookup & "' can be found in " & foundIt.Address(0, 0) & ".", vbInformation
    Else
        MsgBox "The lookup value '" & myLookup & "' could not be found.", vbInformation
    End If

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,214,980
Messages
6,122,563
Members
449,088
Latest member
Motoracer88

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