【发布时间】:2018-03-19 23:39:06
【问题描述】:
我有一个销售人员列表,该列表被过滤并将所有过滤后的数据移动到它自己的电子表格中。我遇到的问题是宏也在过滤小计行,因此它创建了一个没有数据的工作表,并且还造成了工作表太大而无法保存文件的情况。
我写了一些我认为可以防止任何以“Sheet”开头的工作表不被过滤的代码,但我不知道如何在字符串中使用通配符。需要通配符,因为“Sheet #”因月份而异。
Dim Sht As Worksheet
Dim Rng As Range
Dim List As Collection
Dim varValue As Variant
Dim E As Long
' // Set your Sheet name
Set Sht = Application.ActiveSheet
' // set your auto-filter, A6
With Sht.Range("A2")
.AutoFilter
End With
' // Set your agent Column range # (2) that you want to filter it
Set Rng = Range(Sht.AutoFilter.Range.Columns(22).Address)
ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Add
Key:=Range _
("V:V"), SortOn:=xlSortOnValues, Order:=xlAscending,
DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
' // Create a new Collection Object
Set List = New Collection
' // Fill Collection with Unique Values
On Error Resume Next
For E = 2 To Rng.Rows.Count
List.Add Rng.Cells(E, 1), CStr(Rng.Cells(E, 1))
Next E
' // Start looping in through the collection Values
For Each varValue In List
' // Filter the Autofilter to macth the current Value
'Rng.AutoFilter Field:=22, Criteria1:=varValue, _
' Operator:=xlAnd, Criteria2:="<>"
Rng.AutoFilter Field:=22, Criteria1:="<>Sheet*", _
Operator:=xlAnd, Criteria2:=varValue
' // Copy the AutoFiltered Range to new Workbook
'If List = (Blanks) Then
Sht.AutoFilter.Range.Copy
Criteria1:="Sheet*" 代码是我尝试做的,上面的代码是以前的。所以我的问题是如何防止创建小计行表?
【问题讨论】:
标签: vba excel filtering copy-paste