我对VBA很新(本周)我正在使用代码
Function myCountIf(rng As Range, criteria) As Long
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Summary-Sheet" And ws.Name <> "Notes" And ws.Name <> "Results" Then
myCountIf = myCountIf + WorksheetFunction.CountIf(ws.Range(rng.Address), criteria)
End If
Next ws
结束功能
但我想使用CountIfS函数来计算多个工作表中单元格值为Yes的次数IF另一个单元格值(也是多个单元格值也是
我试过了:
=myCountIf(AND(I8="Yes",I7="Yes"))
但它不起作用
但:
=myCountIf(I8,"Yes")
工作正常
它可能非常简单,如果是这样,我很抱歉
答案 0 :(得分:0)
这是一个小改动,为您提供两个过滤条件:
Function myCountIfs(rng1 As Range, criteria1, rng2 As Range, criteria2) As Long
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Summary-Sheet" And ws.Name <> "Notes" And ws.Name <> "Results" Then
myCountIfs = myCountIfs + WorksheetFunction.CountIfs(ws.Range(rng1.Address), criteria1, ws.Range(rng2.Address), criteria2)
End If
Next ws
答案 1 :(得分:0)
尝试下面修改过的 UDF :
Option Explicit
Function myCountIfs(Rng1 As Range, Criteria1 As String, Rng2 As Range, Criteria2 As String) As Long
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Select Case ws.Name
Case "Summary-Sheet", "Notes", "Results"
' do nothing
Case Else
myCountIfs = myCountIfs + Application.CountIfs(ws.Range(Rng1.Address), Criteria1, ws.Range(Rng2.Address), Criteria2)
End Select
Next ws
End Function
但是,要从Excel工作表的单元格中调用它,请不要使用常规的=myCountIf(AND(I8="Yes",I7="Yes"))
,而是使用以下行:
=myCountIfs(I8,"Yes",I7,"Yes")
请参阅下面的屏幕截图,例如在Excel中使用此 UDF ,所有工作表都有&#34; YES&#34;在单元格I7和I8中,但是其中一个表格的名称是&#34;注意&#34;,因此结果是2(而不是3)。