Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Excel VBA Find function to search specific range only

Posted on 2012-03-24
5
361 Views
Last Modified: 2012-03-24
I have a piece of code which succesfully searches for a string and returns the row but I need to refine it so that it only searches the specified range from top to bottom.  If nothing is found then it should return "".

check and x are defined further up...

error1 = Cells.Find(What:=check, After:=Range("A" & x), LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Row

Open in new window

0
Comment
Question by:simonwait
  • 2
  • 2
5 Comments
 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
ID: 37760996
What is the specified range?

If we assume it's A1:A100 you could search only that range like this.
Set rngFnd =  Range("A1:A100").Find(What:=check, After:=Range("A" & x), LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False)

If rngFnd Is Nothing Then
    error1 = ""
Else
    error1 = rngFnd.Row

Open in new window

0
 
LVL 42

Expert Comment

by:dlmille
ID: 37761578
FYI - the AFTER range is the LAST cell that is searched.  If order is important, you have to specify the last cell in the range you want to search, not the first.  But, again, only if search order is important.

Cheers.

Dave
0
 
LVL 18

Expert Comment

by:p912s
ID: 37761725
If you want it to only search the selected cells you could change your code like this.

error1 = Selection.Find(What:=check, After:=ActiveCell, LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Row


HTH

Scot
0
 
LVL 33

Expert Comment

by:Norie
ID: 37761731
Dave has a good point, and I was wondering what to do with the After argument.

If you wanted to search the range I showed in the example starting at the first cell, A1, the code would look like this.
Set rngFnd =  Range("A1:A100").Find(What:=check, After:=Range("A1"), LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False)

If rngFnd Is Nothing Then
    error1 = ""
Else
    error1 = rngFnd.Row

Open in new window

0
 
LVL 42

Expert Comment

by:dlmille
ID: 37761765
imnorie - that particular find would search Range("A1") as the LAST parameter, not the first.  The AFTER (I learned recently) is the LAST element to be tested.  Not that it matters, UNLESS, order is important.

Cheers,

Dave
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question