如何从excel vba中的Find函数获取单元格地址

Ran*_*ama 3 excel vba find

我有一个问题,如何使用find函数获取单元格地址.这是代码

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
Run Code Online (Sandbox Code Playgroud)

问题是当我尝试调试代码时,"Found"变量包含一个"字符串"而不是单元格地址.

不知道我的代码有什么问题.

nig*_*r23 9

看起来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
Run Code Online (Sandbox Code Playgroud)

  • `Find`可以返回`Nothing`,所以在它的末尾添加`.Address`就是[只是要求运行时错误91](http://stackoverflow.com/search?q=%5Bexcel-vba% 5D +发现+误差+ 91).`find`应始终设置为临时的`Range`对象,并在使用任何属性之前对`Nothing`进行测试. (3认同)

Jam*_*ves 5

我在互联网上找不到这个。此代码将为您提供行和列。

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
Run Code Online (Sandbox Code Playgroud)