vba 过滤vba后如何只取一些列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5922159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to take only some columns after filtering vba?
提问by vovkjn
Hei,
嘿,
I have 2 sheets in my excel and would like to take from 1 sheet where i have customer information to 2 sheet where i would like to have filtered data.
我的 excel 中有 2 张工作表,我想从我有客户信息的 1 张工作表中取出我想要过滤数据的 2 张工作表。
So i did this macro for having filtered data on the sheet 2.
所以我做了这个宏来过滤工作表 2 上的数据。
`Sub choice()
'
' choice Macro
'
Dim parameter As Range, data As Range, result As Range
Set parameter = Range("param").CurrentRegion
Set data = Range("table").CurrentRegion
Set result = Range("result").CurrentRegion.Offset(1, 0)
result.ClearContents
Set result = Range("result").CurrentRegion
data.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=parameter _
, CopyToRange:=result, Unique:=False
End Sub`
data = "table" - customer table on 1 sheet
data = "table" - 1 张纸上的客户表
However it copy pastes me all the data from customer information.
然而,它复制粘贴了我来自客户信息的所有数据。
Is it possible somehow add code in this macro to take only FirstName and Lastname from there?
是否有可能以某种方式在此宏中添加代码以仅从那里获取名字和姓氏?
Thank you!
谢谢!
回答by osknows
It's probably something to do with the way you've defined the ranges for the data, criteria and destination. The code below should help showing explicit ranges.
这可能与您定义数据、标准和目标范围的方式有关。下面的代码应该有助于显示明确的范围。
Assume you have data to filter in Sheet1 columns A:F
, criteria to filter are in range I1:N2
and the filtered data to be copied into columns P:U
假设您在 Sheet1 列中有要过滤的数据,要过滤的A:F
条件在范围内I1:N2
,并且过滤后的数据要复制到列中P:U
The first line of the code will only place columns E:F
into columns T:U
based on criteria in I1:N2
The second line will place all of the filtered data from A:F
into P:U
based on same criteria.
代码的第一行将仅根据中的条件将列E:F
放入列T:U
中I1:N2
,第二行将根据相同的条件将所有过滤后的数据A:F
放入P:U
。
Also, ensure that the field/column headings match exactly for data/criteria/destination
此外,确保字段/列标题与数据/标准/目的地完全匹配
Sub test()
'Returns E:F in destination range T:U based on filter criteria
Sheet1.Columns("A:F").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:= _
Sheet1.Range("I1:N2"), _
CopyToRange:=Sheet1.Range("T1:U1"), Unique:=True
'Returns full range A:F in destination range P:U based on filter criteria
Sheet1.Columns("A:F").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:= _
Sheet1.Range("I1:N2"), _
CopyToRange:=Sheet1.Range("P1:U1"), Unique:=True
End Sub