2011-09-08 108 views
0

下面的宏將允許我在工作表1中的標題中找到一個名稱,並將整個列複製到工作表2中。現在我想繼續代碼,但是面臨一個問題,我會嘗試解釋。使用VBA在Excel中設置過濾器

Sub CopyColumnByTitle() 
'Find "Name" in Row 1 
    With Sheets(1).Rows(1) 
    Set t = .Find("Name", lookat:=xlpart) 
'If found, copy the column to Sheet 2, Column A 
'If not found, present a message 
    If Not t Is Nothing Then 
     Columns(t.Column).EntireColumn.Copy _ 
      Destination:=Sheets(2).Range("A1") 
     Else: MsgBox "Title Not Found" 
    End If 
    End With 
End Sub 

所有數據之後在片材2如下面粘貼....

Sheet 2 
Name Age Address Date of Birth 
John 25 US 1-Sep-11 
Hary 26 US 1-Sep-11 
John 27 UK 1-Sep-11 
Hary 28 US 2-Sep-11 
King 29 UK 3-Sep-11 
Peter 30 US 3-Sep-11 

我需要設置的過濾器,如下所示和所述濾波的數據如上述代碼做複製到片材3:

  1. 我需要設置表2的過濾條件,這有助於我看到Name S中的等於「約翰」或「HARY」複製和整個數據粘貼到表3
  2. 我需要設置另一個篩選器,其中Name等於「John」,而Date of Birth等於「1-Sep-11」(注意日期應始終爲昨天)。複製和整個 數據成片。4.
  3. 粘貼在第三次,我需要設置一個過濾器,Name等於「王」,並複製和過去的整個數據成片5

非常感謝John的回覆,您給出的回覆很有效,但由於緊急要求我已經設計了我的代碼。

我需要相同的幫助。我正在粘貼部分代碼,因爲無法粘貼整個代碼。

該代碼允許我將數據從一個工作簿複製到另一個工作簿,但在複製數據時,我需要複製整個列,因爲其中有一些空白單元格。所以如果我不使用.EntireColumn,宏不會複製空白單元格之後的單元格。現在,在將數據粘貼到其他工作簿的同時,我需要粘貼它,而不需要標題。

如果你能幫我解決這個問題,我將不勝感激。

Windows("macro2.xlsm").Activate 
Range(Range("M2"), Range("N2").End(xlDown)).EntireColumn.Select 
Application.CutCopyMode = False 
Selection.Copy 
Windows("formula.xls").Activate 
Range(Range("I2"), Range("J2").End(xlDown)).EntireColumn.Select 
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _ 
xlNone, SkipBlanks:=False, Transpose:=False 
+0

我不認爲你的第一個代碼將工作正常(有一些語法錯誤)。如何在第三張和第四張紙上過濾數據?我們是否在代碼中設置了過濾器* John? – JMax

回答

2

任務1:

thisworkbook.sheets(2).activate 
activesheet.range("A:A").select 'set column you filter for probable names here 
Selection.AutoFilter Field:=1, Criteria1:="=John", Operator:=xlOr, _ 
Criteria2:="=Hary" ' filters only for hary or john 
activate.usedrange.select ' now select the filtered sheet to copy 
selection.copy 
ActiveSheet.ShowAllData ' now retain back the data so that you get your original file 
thisworkbook.sheets(3).activate 'select your sheet3 and paste it 
activate.range("A1").select 
activesheet.paste 

任務2:

thisworkbook.sheets(2).activate 
activesheet.range("A:A").select \\'set column you filter for probable names here 
Selection.AutoFilter Field:=1, Criteria1:="John" \\ filters for john 
Selection.AutoFilter Field:=2, Criteria1:="1-sep-2011" \\ filters for date only for john rows 
activate.usedrange.select ' now select the filtered sheet to copy 
selection.copy 
ActiveSheet.ShowAllData ' now retain back the data so that you get your original file 
thisworkbook.sheets(4).activate 'select your sheet3 and paste it 
activate.range("A1").select 
activesheet.paste 

任務3

thisworkbook.sheets(2).activate 
activesheet.range("A:A").select 'set column you filter for probable names here 
Selection.AutoFilter Field:=1, Criteria1:="=King" ' filters only king 
activate.usedrange.select ' now select the filtered sheet to copy 
selection.copy 
ActiveSheet.ShowAllData ' now retain back the data so that you get your original file 
thisworkbook.sheets(5).activate 'select your sheet3 and paste it 
activate.range("A1").select 
activesheet.paste 

也許它可能給你一些想法如何做到這一點。任何更多的疑問隨時問我。

謝謝!你可能會去複製目標:=和更多的方式來做到這一點。實際上我現在得走了,所以我只給了你一個樣片。