Solved

Excel VBA Find function to search specific range only

Posted on 2012-03-24
5
351 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 41

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 41

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

776 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