用VBA在Excel中设置filter

下面的macros将允许我在表1的标题中find一个名称,并将整个列复制到表2.现在我想继续代码,但是面临一个问题,我将尝试解释。

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 

我需要如下所示设置filter,并将过滤后的数据复制到表单3中,如上所示:

  1. 我需要在表2中设置筛选条件,这可以帮助我查看Name等于“John”或“Hary”的名称,并将整个数据复制并粘贴到工作表3中。
  2. 我需要设置另一个filter, Name等于“约翰”和Date of Birth等于“1 – 9月11日”(注意date应该始终是昨天)。 将整个数据复制并粘贴到工作表4中。
  3. 第三次,我需要设置一个Name等于“国王”的filter,并复制和超过整个数据到表5。

谢谢你的回复,你给的回复是有效的,但是由于迫切的要求,我已经devise了我的代码。

我需要一些帮助。 我正在粘贴部分代码,因为无法粘贴整个代码。

该代码允许我将数据从一个工作簿复制到另一个工作簿,但是在复制数据时,我需要复制整个列,因为其中有一些空白单元格。 所以如果我不使用.EntireColumn ,macros不会复制空白单元格之后的单元格。 另外现在,在将数据粘贴到其他工作簿的同时,我需要将其粘贴而不用标题。

如果你帮我解决这个问题,我将不胜感激。

 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 

任务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 

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

谢谢! 你可能会去复制目的地:=和更多的方式来做到这一点。 其实我现在必须走了,所以我只是给你一个样本作品。