标签:array mod use count term sed operator edr ext
场景:在Excel工作簿中有A、B、C、D、E五张工作表,它们具有相同的表结构(每张表的前两列都用来筛选)。现对工作表A手动进行筛选后执行宏,以便自动对工作表B、C、D、E执行与A相同的筛选。
Sub Macro1()
If Sheets("A").AutoFilterMode Then
‘获取"A"工作表中第1列筛选相关的参数
If Sheets("A").AutoFilter.Filters(1).On Then ‘如果筛选条件不为(全选)
If Sheets("A").AutoFilter.Filters(1).Count = 1 Then ‘筛选条件只包含1个值
cr11 = Sheets("A").AutoFilter.Filters(1).Criteria1
ElseIf Sheets("A").AutoFilter.Filters(1).Count = 2 Then ‘筛选条件只包含2个值
cr11 = Sheets("A").AutoFilter.Filters(1).Criteria1
cr12 = Sheets("A").AutoFilter.Filters(1).Criteria2
op1 = Sheets("A").AutoFilter.Filters(1).Operator
Else ‘筛选条件包含3个或更多的值
cr11 = Sheets("A").AutoFilter.Filters(1).Criteria1
op1 = Sheets("A").AutoFilter.Filters(1).Operator
End If
End If
‘获取"A"工作表中第2列筛选相关的参数
If Sheets("A").AutoFilter.Filters(2).On Then ‘如果筛选条件不为(全选)
If Sheets("A").AutoFilter.Filters(2).Count = 1 Then ‘筛选条件只包含1个值
cr21 = Sheets("A").AutoFilter.Filters(2).Criteria1
ElseIf Sheets("A").AutoFilter.Filters(2).Count = 2 Then ‘筛选条件只包含2个值
cr21 = Sheets("A").AutoFilter.Filters(2).Criteria1
cr22 = Sheets("A").AutoFilter.Filters(2).Criteria2
op2 = Sheets("A").AutoFilter.Filters(2).Operator
Else ‘筛选条件包含3个或更多的值
cr21 = Sheets("A").AutoFilter.Filters(2).Criteria1
op2 = Sheets("A").AutoFilter.Filters(2).Operator
End If
End If
‘将"A"工作表中筛选相关的参数应用到其他几张工作表
arr = Array("B", "C", "D", "E")
For i = 0 To 3
Sheets(arr(i)).Select
Range("A2:B2").Select
Selection.AutoFilter
row_max = Worksheets(arr(i)).UsedRange.Rows.Count + 1
If Sheets("A").AutoFilter.Filters(1).On Then ‘如果第1列不是(全选)
If Sheets("A").AutoFilter.Filters(1).Count = 1 Then ‘筛选条件只包含1个值
ActiveSheet.Range("$A$2:$B$" & row_max).AutoFilter Field:=1, Criteria1:=cr11
ElseIf Sheets("A").AutoFilter.Filters(1).Count = 2 Then ‘筛选条件只包含2个值
ActiveSheet.Range("$A$2:$B$" & row_max).AutoFilter Field:=1, Criteria1:=cr11, Operator:=op1, Criteria2:=cr12
Else ‘筛选条件包含3个或更多的值
ActiveSheet.Range("$A$2:$B$" & row_max).AutoFilter Field:=1, Criteria1:=cr11, Operator:=op1
End If
End If
If Sheets("A").AutoFilter.Filters(2).On Then ‘如果第2列不是(全选)
If Sheets("A").AutoFilter.Filters(2).Count = 1 Then ‘筛选条件只包含1个值
ActiveSheet.Range("$A$2:$B$" & row_max).AutoFilter Field:=2, Criteria1:=cr21
ElseIf Sheets("A").AutoFilter.Filters(2).Count = 2 Then ‘筛选条件只包含2个值
ActiveSheet.Range("$A$2:$B$" & row_max).AutoFilter Field:=2, Criteria1:=cr21, Operator:=op2, Criteria2:=cr22
Else ‘筛选条件包含3个或更多的值
ActiveSheet.Range("$A$2:$B$" & row_max).AutoFilter Field:=2, Criteria1:=cr21, Operator:=op2
End If
End If
Next i
End If
End Sub
标签:array mod use count term sed operator edr ext
原文地址:https://www.cnblogs.com/oddgod/p/13067488.html