VBA for using find feature

normpam

Active Member
Joined
Oct 30, 2002
Messages
360
I previously posted this, but do not get the results I was hoping for with the 'fix', and I have a question about it. Any help is greatly appreciated!

I've written some code that will ask the user for a sheet name; go to the sheet, and find an item. If it finds it, I direct the program to go back to the first sheet and put the word "Match" somewhere. No problem. But, if the item is NOT found, then my code generates an error,

"Object variable or With Block variable not set"

How can I work around this? All I want to do is write my code so that it loops through a list and searches for items on another sheet.

The following response was posted:

Dim foundItem As Range
Set foundItem = Cells.Find("xxx")
If Not foundItem Is Nothing Then
MsgBox "Found it"
Else
MsgBox "Didn't find it"
End If

First of all, when I incorporated this code, though I specifically used and item that was in both sheets, the result was "Didn't find it". So, it doesn't seem to work. Also, what is the ("xxx") mean in the statement 'set foundItem = Cells.Find("xxx")? Are the X's a variable? Does the result have to be three characters?
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Also, what is the ("xxx") mean in the statement 'set foundItem = Cells.Find("xxx")?

It is supposed to find (literally) "xxx"

Are the X's a variable

Not in this case though it can be a variable if desired. The quotation marks designate a literal as opposed to a variable.


Does the result have to be three characters?

No. There may be some kind of a limit but it would probably be the same number of characters that a single cell can hold. Not sure what that number is.

There are quite a few parameters you can use with the "Find" method. Consider or ignore case, match entire cell or part of a cell etc. See VBA help for complete details.

If you post your real code I'm sure someone can help you with it.

Gary
 
Upvote 0
The xxx needs to be replaced by the string you're looking for. It appears that it was just an example, since you didn't specify in the original post exactly what you were looking for. Post the code as you amended it (assuming you did)
 
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,704
Members
452,938
Latest member
babeneker

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