我很新VBA 我有一個Excel範圍的細胞將不斷自動過濾和一個只考慮可見(過濾)細胞的COUNTIFS UDF幫助我很大。 我發現這個代碼似乎是我需要的,但它只適用於COUNTIF,而不適用於COUNTIFS 如果有人對如何修改它以使其成爲COUNTFS,有任何想法,那將是超級大氣!VBA修改Excel COUNTIFS UDF
Function Vis(Rin As Range) As Range
'Returns the subset of Rin that is visible
Dim Cell As Range
Application.Volatile
Set Vis = Nothing
For Each Cell In Rin
If Not (Cell.EntireRow.Hidden Or Cell.EntireColumn.Hidden) Then
If Vis Is Nothing Then
Set Vis = Cell
Else
Set Vis = Union(Vis, Cell)
End If
End If
Next Cell
End Function
Function COUNTIFv(Rin As Range, Condition As Variant) As Long
'Same as Excel COUNTIF worksheet function, except does not count
'cells that are hidden
Dim A As Range
Dim Csum As Long
Csum = 0
For Each A In Vis(Rin).Areas
Csum = Csum + WorksheetFunction.CountIf(A, Condition)
Next A
COUNTIFv = Csum
End Function
你有沒有考慮過使用'SUBTOTAL' - 這可以應用一系列的數學函數(包括計數)到一個範圍,並可以選擇處理所有或僅可見的單元格 –
不,我沒有,但我會試一試!任何指針? – bibs