gpt4 book ai didi

excel - 如果有任何失败,如何显示所有失败,如果没有,则显示没有失败框

转载 作者:行者123 更新时间:2023-12-04 21:49:57 26 4
gpt4 key购买 nike

我想用对应的样本显示失败的 MsgBox。如果不显示另一个没有失败的 MsgBox。

我觉得我快到了,但有些事情搞砸了。

如果我将 MsgBox 放在循环中,则 MsgBox 出现不止一次,如果我把它放出来,它会显示 MsgBox 的“失败”(如果有)和“没有失败”

我怎样才能用(If-statement)只显示其中一个,当然也显示一次。显示全部失败的框或显示没有的框。

我运行的代码:

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

最后,正确缩进你的代码使它更容易阅读。

关于excel - 如果有任何失败,如何显示所有失败,如果没有,则显示没有失败框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56529025/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com