如何获取在此代码中找到该值的行号的值?

时间:2019-06-26 12:31:07

标签: vba

如何在以下代码中获取在其中找到值的范围或行号? 在“如果”部分中,我想要行号,而不是转到这段代码中的值

With wsSource.Range("A:A") 'searches all of column A
      Set Rng1 = .Find(What:=FindString, _
      After:=.Cells(.Cells.Count), _
      LookIn:=xlValues, _
      LookAt:=xlWhole, _
      SearchOrder:=xlByRows, _
      SearchDirection:=xlNext, _
      MatchCase:=False)
      If Not Rng1 Is Nothing Then
           Application.Goto Rng1, True 'value found
      Else
           MsgBox "Nothing found" 'value not found
      End If
End With

1 个答案:

答案 0 :(得分:0)

激活Rng1,然后获得如下所示的活动单元格行。

With wsSource.Range("A:A") 'searches all of column A
      Set Rng1 = .Find(What:=FindString, _
      After:=.Cells(.Cells.Count), _
      LookIn:=xlValues, _
      LookAt:=xlWhole, _
      SearchOrder:=xlByRows, _
      SearchDirection:=xlNext, _
      MatchCase:=False)
      If Not Rng1 Is Nothing Then
           'Your code
           Rng1.Activate
           'Get Row number
           MsgBox ActiveCell.Row
           'Get Row value
           MsgBox ActiveCell.Value
      Else
           MsgBox "Nothing found" 'value not found
      End If
End With