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.
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
Find
can returnNothing
, so tacking.Address
onto the end of it is just begging for a run-time error 91.Find
should always be set to a temporaryRange
object and tested againstNothing
before any of the properties are used.Hi @nightcrawler23 thanks for your feedback. Is there any way to get the row and column from "ra.Address" seperately? I want to apply it into cells selection (Ex: Cells(1,2))
A Range has row and column properties. So you can use ra.Row and ra.Column to get the values. msdn.microsoft.com/en-us/library/office/ff838238.aspx#Anchor_3
@RandyAdhitama when you type the
.
dot afterra
, there should be a little in-place dropdown (that's IntelliSense) showing you all the available members. Explore a bit. Research the members online when you're not sure what they do - everything is thoroughly documented. You can also use the object browser (F2 in the VBE editor) to browse all the types and members in all referenced libraries.