Using 'find' in VBA code

JimM

Well-known Member
Joined
Nov 11, 2003
Messages
752
Guys

I'm trying to write some code that will search a sheet for variable1 and then assign the row number that the result is in to variable2

Got as far as

variable2 = Sheets("Sheet1").Range("J:J").Find(What:=variable1, LookAt:=xlPart).Row


However if variable1 isn't found it bombs out. How would I go about setting variable2 to 1000 when this happens

Thanks

Jim
 

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.
Try like this

Code:
Dim Found As Range
Set Found = Sheets("Sheet1").Range("J:J").Find(What:=variable1, LookAt:=xlPart)
If Found Is Nothing Then
    variable2 = 1000
Else
    variable2 = Found.Row
End If
 
Upvote 0
Code:
set variable2 = Sheets("Sheet1").Range("J:J").Find(What:=variable1, LookAt:=xlPart)

If variable2 Is Nothing Then ' whatever you want to do
hope this helpssssssssss
 
Upvote 0
Try:

Rich (BB code):
Sub Foo()
variable1 = "Test"  << Change to your variable1
On Error GoTo errhandler
Variable2 = Sheets("Sheet1").Range("J:J").Find(What:=variable1, LookAt:=xlPart).Row
MsgBox "Found in Row " & Variable2
Exit Sub
errhandler:
Variable2 = 1000
MsgBox "Didn't Find variable1, so assigned " & Variable2
On Error GoTo 0
End Sub
 
Upvote 0
Try:

Rich (BB code):
Sub Foo()
variable1 = "Test"  << Change to your variable1
On Error GoTo errhandler
Variable2 = Sheets("Sheet1").Range("J:J").Find(What:=variable1, LookAt:=xlPart).Row
MsgBox "Found in Row " & Variable2
Exit Sub
errhandler:
Variable2 = 1000
MsgBox "Didn't Find variable1, so assigned " & Variable2
On Error GoTo 0
End Sub


why use error handler when you can set the variable?
I think error handler aren't that safe.
 
Upvote 0
Different strokes, for different folks;
error handling such as this is used extensively, according to what I've seen.
 
Upvote 0

Forum statistics

Threads
1,224,504
Messages
6,179,144
Members
452,891
Latest member
JUSTOUTOFMYREACH

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