在消息框中显示 COUNTIF 结果

Showing COUNTIF results in a Message Box

我目前正在使用此代码显示在列中找到的 "RCA Pending" 的数量。消息框确实显示了它在列中找到的正确次数,但是,它为每个实例创建了一个框(即,如果列中有 2 个实例,当工作簿打开时它将显示 "Found 2 RCA Pending(s)" ,然后当用户单击“确定”时,会出现第二个弹出窗口说同样的事情。如果有 5 个,您将获得 5 个连续的弹出窗口)。

Sub Auto_Open()

    Dim row As Range
    For Each row In Worksheets("Swivel").UsedRange.Rows
        If row.Cells(1, "AB").Value = "RCA Pending" Then
            MsgBox "Found " & WorksheetFunction.CountIf(Columns("AB"), "RCA Pending") & " RCA Pending(s)", vbInformation, "RCA Pending Found"
        End If
    Next row

End Sub

如何更改它以显示实例总数而不是获得多个弹出窗口?

附带说明一下,我使用 UsedRange 是因为范围总是在增长。此代码所在的模块顶部有 Option Explicit

这是您正在尝试的吗?

Sub Auto_Open()
    Dim instances As Long

    instances = WorksheetFunction.CountIf(Columns("AB"), "RCA Pending")

    If instances <> 0 Then _
    MsgBox "Found " & instances & " RCA Pending(s)", vbInformation, "RCA Pending Found"
End Sub

Sub Auto_Open()
    Dim instances As Long

    instances = WorksheetFunction.CountIf(Columns("AB"), "RCA Pending")

    MsgBox "We Found " & instances & " instances of RCA Pending(s)", _
    vbInformation, "RCA Pending Found"
End Sub