在消息框

时间:2016-04-12 18:39:34

标签: excel-vba excel-2007 vba excel

我目前正在使用此代码显示" RCA待定"在专栏中找到。消息框确实显示了在列中找到的正确次数,但是,它为每个实例创建一个框(即,如果列中有2个实例,则当工作簿打开时,它将显示"找到2 RCA Pending(s)",然后当用户点击OK时,会出现第二个弹出窗口,显示相同的内容。如果有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

1 个答案:

答案 0 :(得分:2)

这是你在尝试的吗?

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

OR

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