[英]How do I copy some specific columns from a given range which is auto filtered in Excel VBA?
I have a data set in excel of 71 columns. 我有71列Excel中的数据集。 I need to copy only 7 columns from them after applying auto filter on the source sheet(
RAS(Offshore)
) to the destination sheet( Dst
). 在源工作表(
RAS(Offshore)
)上将自动过滤器应用到目标工作表( Dst
)之后,我仅需要从中复制7列。 The columns that I need to copy are C,D,G,M,AH,BD,BP
after applying the filters on RAS(Offshore)
to Dst
, using Excel VBA. 使用Excel VBA将
RAS(Offshore)
上的筛选器应用于Dst
,需要复制的列为C,D,G,M,AH,BD,BP
。
I am successful in applying the auto filters and copying the whole range, but I am not able to extract particular columns as stated above. 我成功地应用了自动过滤器并复制了整个范围,但是如上所述,我无法提取特定的列。 Please help.
请帮忙。
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
Please suggest how can I copy C,D,G,M,AH,BD,BP
from rng
object. 请建议如何从
rng
对象复制C,D,G,M,AH,BD,BP
。
You can use Intersect
to limit the copy to your specific columns (see code below). 您可以使用“
Intersect
”将副本限制为您的特定列(请参见下面的代码)。 also notice that when applying .Copy
, you dont need to use .SpecialCells(xlCellTypeVisible)
because the Copy
method automatically applies to visible cells only. 还应注意,应用
.Copy
,不需要使用.SpecialCells(xlCellTypeVisible)
因为Copy
方法仅自动应用于可见单元格。
Try it this way: 尝试这种方式:
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
If you have filterd the range, you could use the Copy mode this way: With the statement xlCellTypeVisible you only copy the Filterd Values to the new Sheet. 如果已过滤范围,则可以按以下方式使用“复制”模式:使用语句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)
and so on. 等等。 You can insert this in a Loop aswell.
您也可以将其插入循环中。 Hope this will help you
希望这个能对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.