如何复制在Excel VBA中自动过滤的给定范围中的某些特定列?

我有一个71列excel的数据集。 在源表( RAS(Offshore) )上应用自动filter后,我只需要从目标表单复制7列。 我需要复制的列是C,D,G,M,AH,BD,BP在使用Excel VBA将RAS(Offshore)filter应用于Dst

我成功地应用自动filter和复制整个范围,但我不能提取如上所述的特定列。 请帮忙。

  FilterCriteria = InputBox("What text do you want to filter on?", _ "Enter the filter item.") My_Range.AutoFilter Field:=34, Criteria1:="=" & FilterCriteria My_Range.AutoFilter Field:=7, Criteria1:="=Freshers/TSS" With My_Range.Parent.AutoFilter.Range Set rng = .Offset(1, 0).Resize(.Rows.Count, .Columns.Count) _ .SpecialCells(xlCellTypeVisible) If Not rng Is Nothing Then 'Copy and paste the cells into DestSh below the existing data rng.Copy With DestSh.Range("A" & LastRow(DestSh) + 1) .PasteSpecial Paste:=8 .PasteSpecial xlPasteValues .PasteSpecial xlPasteFormats Application.CutCopyMode = False End With End If 

请build议如何从rng对象复制C,D,G,M,AH,BD,BP

您可以使用Intersect将副本限制到您的特定列(请参阅下面的代码)。 还应注意,在应用.Copy ,您不需要使用.SpecialCells(xlCellTypeVisible)因为Copy方法仅自动应用于可见单元格。

试试这个方法:

 With My_Range .AutoFilter Field:=34, Criteria1:="=" & FilterCriteria .AutoFilter Field:=7, Criteria1:="=Freshers/TSS" Intersect(.Offset(1), .Parent.Range("C:C,D:D,G:G,M:M,AH:AH,BD:BD,BP:BP")).Copy With DestSh.Range("A" & lastrow(DestSh) + 1) .PasteSpecial xlPasteColumnWidths .PasteSpecial xlPasteValues .PasteSpecial xlPasteFormats End With .AutoFilter End With 

如果已经过滤范围,则可以使用复制模式:使用语句xlCellTypeVisible ,只能将过滤值复制到新工作表。

 Dim intLastRow as Integer With Worksheets("Tabelle1") intLastRow = .Cells(.Rows.Count, "C").End(xlUp).Row end with Worksheets("Tabelle1").Range("C1:C" & intLastRow).SpecialCells(xlCellTypeVisible).Copy _ Destination:=Worksheets("Tabelle2").Cells(1, 1) Worksheets("Tabelle1").Range("D1:D" & intLastRow).SpecialCells(xlCellTypeVisible).Copy _ Destination:=Worksheets("Tabelle2").Cells(1, 2) 

等等。 你可以插入一个循环以及。 希望对你有帮助