Warm tip: This article is reproduced from serverfault.com, please click

How to get cell address from Find function in Excel VBA

发布于 2016-12-07 03:05:10

How do I get cell address using Find function.

Here's the code

Dim Found As Range

Set Found = Worksheets("Sheet 1").Cells.Find(What:="test", LookAt:=xlWhole, MatchCase:=True)

If Not Found Is Nothing Then
    ' do something
End If

When I debug the code, "Found" variable contain a "string" instead of cell address.

Questioner
Randy Adhitama
Viewed
0
nightcrawler23 2016-12-07 11:28:17

It seems you can just use found.address even though it shows as string. The below code worked for me.

Sub findCellAddress()

    Dim ra As Range

    Set ra = Cells.Find(What:="fff", LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False)

    If ra Is Nothing Then
        MsgBox ("Not found")
        Else
        MsgBox (ra.Address)
    End If

End Sub