2013-08-02 34 views
0

我希望我的宏能夠搜索相關文本的特定行,以便它只搜索該行而不搜索其他行。例如,當你輸入「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 

我該如何做到這一點?

+0

以及您的問題在哪裏? –

+0

我似乎無法創建在特定行中進行搜索的功能。它只是剔除錯誤。 – user2645253

+1

'E'行不存在 - 你的意思是你想在'Dave'的每一行中搜索E列嗎? – MattCrum

回答

1

如果您正在尋找E列中的「Dave」,請使用以下代碼。此代碼僅供參考。如果發現戴夫在E列,它進入塔上的確認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 

如果你是不是在一個特定的列找到「戴夫」,那麼請讓我們知道您在哪裏嘗試找到。

希望這會有所幫助。 Vikas B