如何搜索一行

时间:2013-08-02 09:15:06

标签: excel excel-vba row vba

我希望我的宏能够搜索特定行以查找相关文本,以便它只搜索该行而不搜索其他行。例如,当您输入“dave”时,它只应在行E中搜索“dave”。

Dim Answer, Reply
Dim b As Range

Answer = Application.InputBox("Enter the text to search for.", "Search Tool")

With Rows

  Set c = .Find(Answer, LookIn:=xlValues)

    If Not c Is Nothing Then
        firstAddress = c.Address
          Do
          Reply = MsgBox("Has this piece been edited? " & c.Address & _
          " which has a value of " & c.Value & "?", vbQuestion + _
          vbYesNoCancel, "Cell Hi-Liter")

            If Reply = vbYes Then
             c.Select
             Selection.Copy
             Selection.Offset(0, 1).Select
             ActiveSheet.Paste
             Exit Do
            End If

             Set c = .FindNext(c)
             Loop While Not c Is Nothing And c.Address <> firstAddress

                Else
                   MsgBox "Your search text was not found.", vbOKOnly, "Text Not Found"
      End If

End With


End Sub

我该如何做到这一点?

1 个答案:

答案 0 :(得分:1)

如果您要在E列中找到“Dave”,请使用以下代码。此代码仅供参考。如果它在E列中找到Dave,它将在F列上输入确认。

Sub FindDave()
    Dim rngToFind As Range

    Set rngToFind = ActiveSheet.Columns("E:E")

    Dim c As Range
    Dim firstAddress As String

    With rngToFind
        Set c = .Find("Dave", LookIn:=xlValues)
        If Not c Is Nothing Then
            firstAddress = c.Address
            Do
                c.Offset(0, 1).Value = "Found on my left"
                Set c = .FindNext(c)
            Loop While Not c Is Nothing And c.Address <> firstAddress
        End If
    End With

End Sub

如果您不希望在特定专栏中找到“Dave”,请告诉我们您的确切位置。

希望这会有所帮助。 Vikas B