vba countifs函数
VBA中的CountIfs函数是用来计算符合多个条件的单元格数量的函数。它可以根据多个条件计算单元格中符合条件的数量,非常实用。下面是一个全面详细的函数:
```
Function CountIfs(rng As Range, ParamArray criteria() As Variant) As Long
'定义变量
Dim i As Long, j As Long, k As Long
Dim count As Long
Dim match As Boolean
'循环遍历每个单元格
For i = 1 To rng.Rows.Count
For j = 1 To rng.Columns.Count
'判断该单元格是否符合所有条件
match = True
For k = LBound(criteria) To UBound(criteria) Step 2
If Not IsNumeric(criteria(k + 1)) Then
'如果条件不是数字,则进行字符串比较
If Not StrComp(CStr(rng(i, j)), CStr(criteria(k + 1)), vbTextCompare) = criteria(k) Then
match = False
Exit For
End If
Elsecountifs函数怎么输入条件
'如果条件是数字,则进行数字比较
Select Case criteria(k)
Case xlEqual
If Not CDbl(rng(i, j)) = CDbl(criteria(k + 1)) Then
match = False
Exit For
End If
Case xlNotEqual
If Not CDbl(rng(i, j)) <> CDbl(criteria(k + 1)) Then
match = False
Exit For
End If
Case xlLess
If Not CDbl(rng(i, j)) < CDbl(criteria(k + 1)) Then
match = False
Exit For
End If
Case xlGreater
If Not CDbl(rng(i, j)) > CDbl(criteria(k + 1)) Then
match = False
Exit For
End If
Case xlLessEqual
If Not CDbl(rng(i, j)) <= CDbl(criteria(k + 1)) Then
match = False
Exit For
End If
Case xlGreaterEqual
If Not CDbl(rng(i, j)) >= CDbl(criteria(k + 1)) Then
match = False
Exit For
End If
End Select
End If
Next k
'如果所有条件都符合,则计数器加一
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论