Cells.find restricted to a single range??

  • Thread starter Thread starter Steff_DK
  • Start date Start date
S

Steff_DK

I have the below code to search for a number in my sheet.

However, I only want to search the range "rngtest", and if the numbe
does not exist in that range, it should return only a msgbox saying n
such entry.

What should be changed???


Dim iSpecific As integer
Cells.Find(What:=iSpecific, After:=Range("A1"), LookIn:=xlFormulas
LookAt _
:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext
MatchCase _
:=False, SearchFormat:=False).Activate

Cheers

Stef
 
Steff,
replace "Cells.Find" with the following, if "rngtest" is a
variable in your code:

rngtest.find

If "rngtest" is a named range in the workbook:

Range("rngtest").find

Cheers, Pete.
 
What if there's no such entry in the range?

I get runtime error if I search for a number that isn't in the range..
 
Steff,

Try writing your code like

Dim FoundCell As Range
Set FoundCell = RngTest.Find(...)
If FoundCell Is Nothing Then
' no value found in RngTest
Else
FoundCell.Activate
End If

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
I get error 424 object req'd...

Dunno why :-/


Dim FoundCell As Range
Set FoundCell = Range("case").Find(What:=iSpecific, After:=Range("A1")
LookIn:=xlFormulas, LookAt _
:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext
MatchCase _
:=False, SearchFormat:=False).Activate

If FoundCell Is Nothing Then
MsgBox ("Number doesn't exist.")
Else
FoundCell.Activat
 
Remove the .Activate at the end of your .Find() method...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top