将变量传递给 cells.find 并将单元格地址作为结果传递给 vba

pass variable to cells.find and cell address as result in vba

我如何将变量传递给 cells.find 并返回单元格地址作为结果?

 For Each cellName In activeListBoxelements

    Cells.Find(What:=cellName, LookIn:=xlFormulas, LookAt _
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False, SearchFormat:=False).Select

    CellAddr = Selection.Address(False, False, xlR1C1)

    MsgBox (CellAddr)
Next cellName 

我哪里做错了。

谢谢

您需要检查 .Find 是否返回任何内容,然后显示地址。

这是您正在尝试的吗? (未测试)

Dim aCell As Range, cellName As Range, activeListBoxelements As Range
Dim CellAddr As String

'
'~~> Rest of the code
'

For Each cellName In activeListBoxelements
    Set aCell = Cells.Find(What:=cellName.Value, LookIn:=xlFormulas, _
                           LookAt:=xlPart, SearchOrder:=xlByRows, _
                           SearchDirection:=xlNext, MatchCase:=False, _
                           SearchFormat:=False)

    '~~> Check if found
    If Not aCell Is Nothing Then
        CellAddr = aCell.Address(False, False, xlR1C1)
        MsgBox CellAddr
        Set aCell = Nothing
    End If
Next cellName