2016-06-30 47 views
0

我試圖使用userforum搜索特定關鍵字並選擇包含該信息的所有單元格,並將整行復制並粘貼到另一個表格中。到目前爲止,我只知道如何一次選擇一個單元格/行。這是我迄今爲止所擁有的。在vba中搜索並選擇多個單元格

Private Sub CommandButton1_Click() 
    Cells.Find(What:=searchbox1.Text, After:=Range("A1"), LookIn:=xlFormulas, _ 
     LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
     MatchCase:= True, SearchFormat:=False).Activate 
    ActiveCell.EntireRow.Select 
    Selection.Copy 
    sheets(2).activate 
+0

看到我的回答[這個問題](http://stackoverflow.com/questions/38105313/deleting-rows-of-data-not-needed/38105606#38105606)....非常相似的概念 –

回答

0

除非您試圖過濾數百行數百行,否則一個簡單的循環將執行與查找相同的工作。

將此代碼調整到您的工作簿,它將循環顯示您想要測試的範圍,並將您所選擇的範圍內的每一個匹配項複製到textbox

Dim rng As Range 
    Dim destRng As Range 

    Set rng = Range("A1:A20") 

    For Each r In rng 
     If r.Value = searchbox1.Text Then 
     If Not destRng Is Nothing Then 
      Set destRng = Union(r.EntireRow, destRng) 
     Else 
      Set destRng = r.EntireRow 
     End If 
     End If 
    Next r 
     destRng.Copy sheets(2).Range("A1") 
相關問題