Excel VBA返回找到的行

时间:2014-01-24 17:56:39

标签: excel vba

现在我有这个:

        Range("B20:B60000").Select
        Selection.Find(What:=currentPerson, After:=ActiveCell, LookIn:= _
        xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
        xlNext, MatchCase:=False, SearchFormat:=False).Activate

选择特定范围的单元格并查找currentPerson(包含人名的变量)。我如何制作它以便我现在可以使用他的细胞作为参考并获得他上方的行?

1 个答案:

答案 0 :(得分:3)

您可以使用以下代码:

Dim res As Range

Set res = Range("B20:B60000").Find(What:=currentPerson, LookIn:= _
    xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
    xlNext, MatchCase:=False, SearchFormat:=False)

If Not res Is Nothing Then
    MsgBox "Address: " & res.Address
    MsgBox "Row: " & res.Row
    MsgBox "Cell above: " & Cells(res.Row - 1, res.Column).Address
    MsgBox "Entire row above: " & Cells(res.Row - 1, res.Column).EntireRow.Address
Else
    MsgBox "Nothing found"
End If