简体   繁体   English

VBA:CountIfS函数,用于计算多个工作表中单元格值为Yes的次数IF另一个单元格值

[英]VBA: CountIfS function to count how many times a cell value is Yes across multiple worksheets IF another cell value

I'm very new to VBA (this week) and I'm using a code 我是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

End Function 结束功能

But I want to use the CountIfS function to count how many times a cell value is Yes across multiple worksheets IF another cell value (also across multiple is also Yes 但我想使用CountIfS函数计算多个工作表中单元格值为是的次数IF另一个单元格值(也是多个也是

I've tried: 我试过了:

=myCountIf(AND(I8="Yes",I7="Yes"))

but it doesn't work 但它不起作用

but: 但:

=myCountIf(I8,"Yes")

works fine 工作正常

its probably very simple and if so I'm sorry 它可能非常简单,如果是这样,我很抱歉

This is a small change to give you two filter criteria: 这是一个小改动,为您提供两个筛选条件:

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

Try the modified UDF below: 尝试下面修改的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

However, in order to call it from Excel sheet's cells, don't use the regular =myCountIf(AND(I8="Yes",I7="Yes")) but the line below : 但是,为了从Excel工作表的单元格中调用它,请不要使用regular =myCountIf(AND(I8="Yes",I7="Yes"))但下面的行:

=myCountIfs(I8,"Yes",I7,"Yes")

See screen-shot below for example of using this UDF in Excel, all sheets have "YES" in cells I7 and I8, but one of the sheet's name is "Note", therefore the result is 2 (and not 3). 有关在Excel中使用此UDF的示例,请参见下面的屏幕截图,单元格I7和I8中的所有工作表都为“YES”,但是其中一个工作表的名称为“Note”,因此结果为2(而不是3)。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据另一个工作表上的单元格值在多个工作表中隐藏/显示行 - How to hide / show rows across multiple worksheets based on a cell value on another worksheet 如何在所有工作表中获取单元格的值 - How to get the value of a cell across all worksheets VBA - 如果不包含值,如何跨多列清除单元格内容 - VBA - How to Clear Cell Contents across Multiple Columns if Value not Contained 如何使用COUNTIFS VBA函数对两个日期之间的值进行计数? - How to use COUNTIFS VBA Function to count value between two dates? 从另一个工作簿中的某个范围更改多个工作表中单元格的值 - Changing value of a cell in multiple worksheets from a range in another workbook 是否有 excel 公式来计算当另一列中的值为 YES 时 ID 在列中出现的次数? - Is there a excel formula to count how many times an ID appears in a column when the value in another column is YES? 在多个工作表中更新外部单元格引用(使用vba宏) - Updating external cell references across multiple worksheets (using vba macro) 根据另一列中的单元格值命名工作表 - Name worksheets based on cell value in another column VBA:如何通过函数更改另一个单元格的值? - VBA: How to change the value of another cell via a function? 根据单元格值过滤 excel 中的多个工作表 - Filtering multiple worksheets in excel based on the cell value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM