答案一:使用List range,Criteria range,paste range as a range,使用UDF AdvancedFilter。
'Calling Sub
Public Sub CallingSub()
'Declaration of variable as worksheet to contain the worksheet name.
Dim sh As Worksheet, sh1 As Worksheet
'Set the required worksheet
Set sh = Worksheets("Logical operation")
Set sh1 = Worksheets("Transaction")
'Variable declaration for finding the range from ConvertRange sub.
Dim Listrng As Range, criteriarng As Range, targetrng As Range
'Finding the List range from where data will be fetched.
Set Listrng = ConvertRange(sh1, 1, 1, 4, 22)
'Finding the criteria range based on which data will be fetched.
Set criteriarng = ConvertRange(sh, 1, 13, 4, 6)
'Finding the range where data will be copied.
Set targetrng = ConvertRange(sh, 2, 21, 4, 3)
'Calling the AdvancedFitler function.
AdvancedFilter2 Listrng, criteriarng, targetrng
End Sub
'ConvertRange Function
Public Function ConvertRange(ByVal sh As Worksheet, ByVal StartingRow As Long, ByVal StartingCol As Long, ByVal TotalCol As Long, ByVal LastRow As Long) As Range
'Calculating range based on below worksheet.
With sh
'Find the range based on provided data.
Set ConvertRange = .Range(.Cells(StartingRow, StartingCol), .Cells(LastRow, StartingCol + TotalCol - 1))
End With
End Function
'UDF AdvancedFilter
Public Sub AdvancedFilter(Listrng As Range, criteriarng As Range, targetrng As Range)
'Use advanced filter based on data passed on this sub.
Listrng.AdvancedFilter Action:=xlFilterCopy, criteriarange:=criteriarng, copytorange:=targetrng, Unique:=False
End Sub
答案二:在AdvancedFilter中使用List range、Criteria range、paste range as a range以及使用excel。
'Calling Sub
Public Sub CallingSub()
'Declaration of variable as worksheet to contain the worksheet name.
Dim sh As Worksheet, sh1 As Worksheet
'Set the required worksheet
Set sh = Worksheets("Logical operation")
Set sh1 = Worksheets("Transaction")
'Variable declaration for finding the range from ConvertRange sub.
Dim Listrng As Range, criteriarng As Range, targetrng As Range
'Finding the List range from where data will be fetched.
Set Listrng = ConvertRange(sh1, 1, 16, 3, 19)
'Finding the criteria range based on which data will be fetched.
Set criteriarng = ConvertRange(sh, 1, 17, 3, 6)
'Finding the range where data will be copied.
Set targetrng = ConvertRange(sh, 2, 25, 3, 3)
'Use advanced filter function in place..No need to use UDF(User Defined Function).
Listrng.AdvancedFilter Action:=xlFilterCopy, criteriarange:=criteriarng, copytorange:=targetrng, Unique:=False
End Sub
'ConvertRange Function
Public Function ConvertRange(ByVal sh As Worksheet, ByVal StartingRow As Long, ByVal StartingCol As Long, ByVal TotalCol As Long, ByVal LastRow As Long) As Range
'Calculating range based on below worksheet.
With sh
'Find the range based on provided data.
Set ConvertRange = .Range(.Cells(StartingRow, StartingCol), .Cells(LastRow, StartingCol + TotalCol - 1))
End With
End Function
答案三:使用列表范围、条件范围、将范围地址粘贴为字符串及其工作表名称并使用 UDF AdvancedFilter
'Calling Sub
Public Sub CallingSub()
'Declaration of variable as worksheet to contain the worksheet name.
Dim sh As Worksheet, sh1 As Worksheet
'Set the required worksheet
Set sh = Worksheets("Logical operation")
Set sh1 = Worksheets("Transaction")
'Variable declaration for finding the range from ConvertRange sub.
Dim Listrng As Range, criteriarng As Range, targetrng As Range
'Variable declaration for containing the address of range along with sheet name.
Dim a As String, b As String, c As String
'Finding the List range from where data will be fetched.
Set Listrng = ConvertRange(sh1, 1, 1, 4, 22)
'Finding the address of range along with sheet name.
a = "'" & Listrng.Parent.Name & "'!" & Listrng.Address
'Finding the criteria range based on which data will be fetched.
Set criteriarng = ConvertRange(sh, 1, 13, 4, 6)
'Finding the address of range along with sheet name.
b = "'" & criteriarng.Parent.Name & "'!" & criteriarng.Address
'Finding the range where data will be copied.
Set targetrng = ConvertRange(sh, 2, 21, 3, 3)
'Finding the address of range along with sheet name.
c = "'" & targetrng.Parent.Name & "'!" & targetrng.Address
'Calling the AdvancedFitler function.
AdvancedFilter a, b, c
End Sub
'ConvertRange Function
Public Function ConvertRange(ByVal sh As Worksheet, ByVal StartingRow As Long, ByVal StartingCol As Long, ByVal TotalCol As Long, ByVal LastRow As Long) As Range
'Calculating range based on below worksheet.
With sh
'Find the range based on provided data.
Set ConvertRange = .Range(.Cells(StartingRow, StartingCol), .Cells(LastRow, StartingCol + TotalCol - 1))
End With
End Function
'UDF AdvancedFilter
Public Sub AdvancedFilter(Listrng As String, criteriarng As String, targetrng As String)
'Here Listrng,criteriarng and targetrng contain their range address along with sheet name.
'Use advanced filter based on data passed on this sub.
Range(Listrng).AdvancedFilter Action:=xlFilterCopy, criteriarange:=Range(criteriarng), copytorange:=Range(targetrng), Unique:=False
End Sub