2011-09-08 285 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 

也许它可能给你一些想法如何做到这一点。任何更多的疑问随时问我。

谢谢!你可能会去复制目标:=和更多的方式来做到这一点。实际上我现在得走了,所以我只给了你一个样片。