如何计算&从Excel中的自动过滤条件中获取值列表?在为“A”列应用过滤器选项后,您将找到带有复选框的条件,我想获得标准的数量&标准也是如此。我怎么能用VBA代码呢?谢谢:)
答案 0 :(得分:1)
Sub MatchCount()
Dim Criteria(1 To n, 1 To 2) 'Define 2 dimensional array to store criteria and its count, change n as per your requirements
Dim i, j, k As Integer
For k = 1 To n
Criteria(k, 2) = 0
Next k
i = 1
Index = 1
Do While ActiveSheet.Cells(i, 1) <> 0
For j = 1 To n
If Criteria(j, 1) = ActiveSheet.Cells(i, 1) Then 'Counts the criteria
Criteria(j, 2) = Criteria(j, 2) + 1
Exit For
End If
Next j
If j = n+1 Then 'Stores the criteria If it is not found in the array and sets count to one
Criteria(Index, 1) = ActiveSheet.Cells(i, 1)
Criteria(Index, 2) = Criteria(Index, 2) + 1
Index = Index + 1
End If
i = i + 1
Loop
For i = 1 To n 'Print criteria and their respective counts
ActiveSheet.Cells(i, 3) = Criteria(i, 1)
ActiveSheet.Cells(i, 4) = Criteria(i, 2)
Next i
End Sub
答案 1 :(得分:1)
我将此作为半答案添加,因为它包含一些代码,一个链接以及“举手” - 这需要花费很长时间才能完成所有工作出&#39;
首先让我停下来的链接(因为我没有时间):
http://yoursumbuddy.com/autofilter-vba-operator-parameters/
现在我写的代码到目前为止。我们的想法是在表格中每列上方的单元格中输入公式=Filter_Criteria()
,它将列出为该列选择的条件。当我意识到操作员比2003年复杂得多时,我停了下来(如何为初学者列出颜色过滤器)
Public Function Filter_Criteria() As String
Dim rMe As Range
If TypeName(Application.Caller) = "Range" Then
'Where's the function being called from.
Set rMe = Application.Caller
'Is Autofilter on?
If rMe.Parent.AutoFilterMode Then
With rMe.Parent.AutoFilter
'Does the function sit a row above the filtered range?
If Not Intersect(rMe.Offset(1), .Range) Is Nothing Then
With .Filters(rMe.Column - .Range.Column + 1)
If .On Then
'Action depending on type of operator.
Select Case .Operator
'Specific values selected.
Case xlFilterValues
'Date Filter
Case 0
'Selected 'Last Month' in date range.
Case xlFilterDynamic
End Select
End If
End With
End If
End With
End If
End If
End Function
希望这段代码和链接能为您提供一个良好的起点....如果您达到目的,请告诉我们。