我想通过相应的示例显示失败的MsgBox。如果不显示,则显示另一个MsgBox,不会失败。
我觉得我快到了,但是有些混乱。
如果将MsgBox放入循环中,则MsgBox会出现多次,如果我将其显示出来,则会显示MsgBox均显示“失败”(如果有)和“没有失败”
我如何只显示(If语句)其中的一个,并且当然只显示一次。显示所有失败的框或显示不存在的框。
我运行的代码:
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
答案 0 :(得分:3)
您需要向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
最后,正确缩进代码可以使其更易于阅读。