Solved

Excel VBA Find function to search specific range only

Posted on 2012-03-24
5
365 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
[X]
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
  • 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
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 how to use a scrolling table in Microsoft Excel using the INDEX function.

756 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