Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get cell address from Find function in Excel VBA

Tags:

find

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.

like image 986
Randy Adhitama Avatar asked Dec 07 '16 03:12

Randy Adhitama


2 Answers

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
like image 179
nightcrawler23 Avatar answered Nov 01 '22 09:11

nightcrawler23


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, "$")
        ThisRow = Cell_Add(1)
        ThisCol = Cell_Add(2)
    End If
End With
like image 45
James Greaves Avatar answered Nov 01 '22 11:11

James Greaves