如果有则如何显示所有失败,如果没有则显示无失败框

How to show all fails if there is any, if not then show no fail box

我想用相应的示例显示失败的 MsgBox。如果不显示另一个 MsgBox 没有失败。

我觉得我快到了,但有一些乱七八糟的东西。

如果我将 MsgBox 放在循环中,MsgBox 会出现不止一次,如果我把它放出来,它会同时显示 "fails"(如果有)和 "There are no Fails"

的 MsgBox

如何用(If语句)只显示其中一个,当然只显示一次。显示全部失败的框或显示 none.

的框

密码我运行:

Sub Box()
Dim x As Long
Dim fails As String
'Dim passes As String

With Sheet2
    For x = 2 To 8
        If .Range("E" & x).Value > 0.24 Then
        fails = fails & ", " & .Range("A" & x)
        MsgBox "Failed Strut: " & fails
        ElseIf .Range("E" & x).Value < 0.24 Then
        passes = "There are no fails"
        MsgBox passes
        End If
    Next x
End With

'Other attempts
'MsgBox passes
'fails = Right(fails, Len(fails) - 2)
'MsgBox "Failed Strut: " & fails

End Sub

您需要将要显示的范围输入 fails 变量,然后检查您的变量是否为空。此外,无需提供 passes 变量,因为它始终是相同的:

Option Explicit
Sub Box()
    Dim x As Long
    Dim fails As String
    'Dim passes As String

    With Sheet2
        For x = 2 To 8
            If .Range("E" & x).Value > 0.24 Then
                If fails = vbNullString Then
                    fails = .Range("A" & x)
                Else
                    fails = fails & ", " & .Range("A" & x)
                End If
            End If
        Next x
    End With

    'Here you check wether you send one message or the other
    If Not fails = vbNullString Then
        MsgBox "Failed Strut: " & fails
    Else
        MsgBox "There are no fails"
    End If

    'Other attempts
    'MsgBox passes
    'fails = Right(fails, Len(fails) - 2)
    'MsgBox "Failed Strut: " & fails

End Sub

最后,正确缩进您的代码可以使其更易于阅读。