How to take only some columns after filtering vba?
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.
So I did this macro for having filtered data on the sheet 2.
`Sub choice()
'
' choice Macro
'
Dim parameter As Range, data As Range, result As Range
Set parameter = Range("param").CurrentRegion
Set data = Range("table&qu开发者_如何学运维ot;).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
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?
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
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.
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
精彩评论