Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel Find not working

Status
Not open for further replies.

Phantek

Programmer
Apr 25, 2011
29
0
0
US
I used a recorded macro to try and learn how to use the Find command in VBA. Everything works fine if I want to activate the cell containing the information, like this:

Code:
Dim fullName as String

Selection.Find(What:=fullName, After:=ActiveCell, LookIn:= _
        xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext _
        , MatchCase:=False, SearchFormat:=False).Activate

Unfortunately, when I try to assign the address to a variable, it never assigns anything... such as:

Code:
Dim rng as Range, fullName as String

rng = Selection.Find(What:=fullName, After:=ActiveCell, LookIn:= _
        xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext _
        , MatchCase:=False, SearchFormat:=False).Address

Any idea what I'm doing wrong?
 


hi,
Code:
Dim rng as [b]String[/b], fullName as String

rng = Selection.Find(What:=fullName, After:=ActiveCell, LookIn:= _
        xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext _
        , MatchCase:=False, SearchFormat:=False).Address


Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
You asked for a way to get the address, and Skip's code should do that for you.

If you do, however, want to assign the found cell to a range variable, keep your initial code and use:

Set rng = Selection. etc

In general to look through all the cells returned by Find:

From the help (with a minor mod):
Example
This example finds all cells on worksheet one that contain the value 2 and changes it to 5.

With Worksheets(1).Cells
Set c = .Find(2, lookin:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Value = 5
Set c = .FindNext(c)
Loop While Not c Is Nothing
End If
End With

Tony
 




Here is the metod that I oftne use for the Find Method...
Code:
dim rFound as range

set rFound = MySheetObject.Cells.Find("Something")

If not rFound is nothing then
  'found soemthing

End if


Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top