One MsgBox To show all passes and fails [duplicate]

这一生的挚爱 提交于 2019-12-11 06:16:06

问题


I am trying to make one message box to include passes and fails of samples.

All Ranges above 0.24 are a fail and below are a pass, and that to show in one box the passes and fails with correspondent sample #

The code below, show the boxes one by one and even incorrectly, some are blank and some not correct.

May you help me with this please. Thanks

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

回答1:


You could accumulate the results in two string variables as shown below, and display the results after the loop has completed. Also, y is set only if the value is smaller than 0.24. You need to set y before the If.

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

End Sub




回答2:


Here is how you can combine all passes and fails within one message box by concatenating strings and providing output only once:

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


来源:https://stackoverflow.com/questions/56525644/one-msgbox-to-show-all-passes-and-fails

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!