Randy Adhitama
Randy Adhitama

Reputation: 227

How to get cell address from Find function in Excel VBA

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.

Upvotes: 6

Views: 140454

Answers (3)

James Greaves
James Greaves

Reputation: 51

I could not find this anywhere on the internet. This code will give you the row and the column.

Dim ThisPos As Range
With Range("A1:J100")
    Set ThisPos = .Find(What:="List_Position", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
    If Not ThisPos Is Nothing Then
        Cell_Add = Split(ThisPos.Address, "$")
        ThisCol = Cell_Add(1)
        ThisRow = Cell_Add(2)
    End If
End With

Upvotes: 5

Roman Szabó
Roman Szabó

Reputation: 1

This code will give you reference style of the cell address.

 Dim SValue As Range
   With Range("D1:D100")

    Set SValue = .Find(What:="Searched Value", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
    
    If Not SValue Is Nothing Then
        Cell_Split_R = Split(SValue.Address(ReferenceStyle:=xlR1C1), "R")
        Cell_Split_C = Split(Cell_Split_R(1), "C")
        SCol = Cell_Split_C(0)
        SRow = Cell_Split_C(1)
        
    End If
End With

Upvotes: 0

nightcrawler23
nightcrawler23

Reputation: 2066

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

Upvotes: 15

Related Questions