我尝试从列表中挑选一些特定信息并将其显示在消息框中。我成功地在一个单独的消息框中显示每个发现,但我希望将它们全部集中在一个消息框中,而不是在例如10个独立的。 我的代码如下所示:
Sub notificator()
Worksheets("Sheet1").Calculate
Dim i As Integer, k As Integer, intValueToFind1 As String,
intValueToFind1 = "RUN"
For i = 1 To 150
If Cells(i, 7).Value = intValueToFind1 And Worksheets("Sheet1").Cells(i, 9).Value = Empty Then
Beep
MsgBox ("The pump " & Cells(i, 1).Value & " is in operation")
End If
Next i
是否有办法(例如通过使用数组或中间存储器)首先收集信息(逐个循环),最后将它提供给显示的消息框?
答案 0 :(得分:1)
您可以构建一个字符串来显示结果。请注意,您应该添加工作表引用,以防活动工作表不符合您的预期。
Sub notificator()
Worksheets("Sheet1").Calculate
Dim i As Long, k As Long, intValueToFind1 As String, s As String
intValueToFind1 = "RUN"
For i = 1 To 150
If Cells(i, 7).Value = intValueToFind1 And Worksheets("Sheet1").Cells(i, 9).Value = Empty Then
'Beep
s = s & "," & Cells(i, 1).Value
End If
Next i
MsgBox ("The pumps " & Mid(s, 2) & " are in operation")
End Sub