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

其他-如何从Excel VBA中的“查找”功能获取单元格地址

(其他 - How to get cell address from Find function in Excel VBA)

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

如何使用查找功能获取单元格地址。

这是代码

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

当我调试代码时,“ Found”变量包含一个“ string”而不是单元格地址。

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

found.address即使它显示为字符串,你似乎也可以使用下面的代码为我工作。

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