How to use a variable in the FIND statement

Bovey44

New Member
Joined
Jul 4, 2020
Messages
13
Office Version
  1. 2016
Platform
  1. Windows
I have he following command in my program:

Sheets("PLN").Range("F2:F30000").Find(what:=hold_key).Select

I've seen where it's possible to use a literal after the what:= as for instance Find(what:="Rose").Select - however, I want to be able to use a variable - as I have in the command where hold_key is the variable.

Is this possible? I keep getting an error "object variable or with block variable not set" I have hold_key setup in a dim statement as a string
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
That is the error you get when it cannot find the value you are looking for.

A better way to handle it is like this:
VBA Code:
    Set rng = Sheets("PLN").Range("F2:F30000").Find(what:=hold_key)
    
    If rng Is Nothing Then
        MsgBox "Cannot find " & hold_key & " in that range"
    Else
        rng.Select
    End If
 
Upvote 0
Solution
That is the error you get when it cannot find the value you are looking for.

A better way to handle it is like this:
VBA Code:
    Set rng = Sheets("PLN").Range("F2:F30000").Find(what:=hold_key)
   
    If rng Is Nothing Then
        MsgBox "Cannot find " & hold_key & " in that range"
    Else
        rng.Select
    End If
Thank you - I appreciate the help.
 
Upvote 0
You are welcome.
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,539
Members
449,088
Latest member
RandomExceller01

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