问题描述
我想通过相应的示例显示失败的MsgBox.如果不显示,则显示另一个MsgBox,不会失败.
I want to show MsgBox of fails with correspondent samples. If non show another MsgBox with no fail.
我觉得我快到了,但是有些混乱.
I feel I am almost there but have something messing.
如果我将MsgBox放入循环中,则MsgBox会出现多次,如果我将其显示出来,则会显示MsgBox的失败"(如果有)和没有失败"
If I put the MsgBox within the loop the MsgBox appears more than once, if I put it out it shows both MsgBox of "fails" if any and "There are no Fails"
我如何只显示(If语句)其中的一个,并且当然只显示一次.显示所有失败的框或显示不存在的框.
How can I show only one of them with the (If-statement), and of course show once. Either box showing all fails or box showing there are 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
推荐答案
您需要使用要显示的范围输入失败
变量,然后检查变量是否为空.另外,也不必输入 pass
变量,因为它始终是相同的:
You need to feed the fails
variable with the ranges you want to show and then check if your variable is empty or not. Also, there is no need to feed a passes
variable because it will always be the same:
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
最后,正确缩进代码可以使代码更易于阅读.
Finally, indenting correctly your code makes it more easy to read.
这篇关于如何显示所有失败(如果有),如果没有则显示“不失败"框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!