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

时间:2015-07-26 20:16:09

标签: excel-vba find cells vba excel

如何将变量传递给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 

我在这里做错了什么。

感谢

1 个答案:

答案 0 :(得分:1)

您需要检查.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