一个 MsgBox 显示所有通过和失败

One MsgBox To show all passes and fails

我正在尝试制作一个消息框来包含样本的通过和失败。

所有高于 0.24 的范围都是失败的,低于 0.24 的范围是通过的,并且在一个框中显示通过和失败以及相应的样本 #

下面的代码,一个一个的显示框,甚至不正确,有些是空白的,有些是不正确的。

请你帮我解决这个问题。谢谢

Sub MsgB()
Dim x As Long
For x = 2 To 8
    If Sheet2.Range("B" & x).Value < 0.24 Then
       y = Sheet2.Range("A" & x).Value
      MsgBox "Pass: " & y
   Else
     MsgBox "Fail: " & y
   End If
Next

End Sub

您可以如下所示将结果累加到两个字符串变量中,并在循环完成后显示结果。此外,仅当值小于 0.24 时才设置 y。您需要在 If.

之前设置 y
Sub MsgB()
Dim x As Long
Dim pass as String
pass = ""
Dim fail as String
fail = ""
For x = 2 To 8
    y = Sheet2.Range("A" & x).Value
    If Sheet2.Range("B" & x).Value < 0.24 Then
        pass = pass & ", " & y
    Else
        fail = fail & ", " & y
    End If
Next
' Print pass and fail, removing the leading ", ".
pass = Right(pass, Len(pass) - 2)
fail = Right(fail, Len(fail) - 2)
MsgBox "Pass: " & pass & vbCrLf & "Fail: " & fail

结束子

下面介绍了如何通过连接字符串并仅提供一次输出来将所有通过和失败合并到一个消息框中:

Sub MsgB()

    Dim x As Long
    Dim passes As String, fails As String

    With Sheet2

        For x = 2 To 8
            If .Range("B" & x).Value < 0.24 Then
                passes = passes & ", " & .Range("A" & x)
            Else
                fails = fails & ", " & .Range("A" & x)
            End If
        Next x

    End With    

    MsgBox "Pass: " & Mid(passes, 3) & vbLf & "Fail: " & Mid(fails, 3)

End Sub