如何将所有消息框添加到VBA中的最终消息框中?

时间:2014-07-29 13:36:56

标签: excel vba excel-vba

任何人都可以帮助我们。我们设法让VBA查看我们的数据并查找已打开超过90天的门票。但目前我们只能这样做,以便每张票都显示为一个sepparate消息框。是否可以将最后的所有结果合并到一个消息框中,该消息框包含结果列表并计算找到的结果总数?我们使用的代码如下。

Sub LongerThan3Months()

Dim lastrow As Long
Dim i As Long
Dim startdate As Date
Dim datenow As Date
Dim Ticket As String
Dim days As Integer
Dim n As Integer

Sheets("Tickets").Select
With ActiveSheet
lastrow = .Cells(.Rows.Count, "B").End(xlUp).Row
End With

datenow = Date

n = 0

For i = 4 To lastrow
startdate = Range("B" & i).Value
days = DateDiff("d", startdate, Now)

If days >= 90 Then

   If Range("B" & i).offset(, 2).Value <> "Closed" Then
       n = n + 1
       Ticket = Range("B" & i).offset(, 3).Value
   End If
End If
Next i

MsgBox (n & " " & "Tickets have been open for over 90 days")

End Sub

2 个答案:

答案 0 :(得分:6)

您将要将resutlts连接成一个字符串。

在循环之外,你应该给自己一个字符串变量。

  Dim msgStr as String
  Dim extraTickets as Long

  extraTickets = 0

然后每次将Ticket字段的串联添加到字符串中。

   If Range("B" & i).offset(, 2).Value <> "Closed" Then
     n = n + 1
     Ticket = Range("B" & i).offset(, 3).Value
     If n < 30 Then
         'this will not happen on the 31st ticket.
         msgStr = msgStr & Ticket & vbcrlf
     End if
   End If

vbcrlfVB的{​​{1}}常量,基本上是输入键。它会在显示屏上为每张票提供自己的一行。

Carriage Return, Line Feed

答案 1 :(得分:1)

试试这个

    Dim msgall As String
    msgall = ""
    ...
    For i = 4 To lastrow
      ...
      If Range("B" & i).Offset(, 2).Value <> "Closed" Then
          N = N + 1
          Ticket = Range("B" & i).Offset(, 3).Value
          msgall = msgall & <the message you want goes here> & vbCrLf
      End If
      ...
    Next i
    ...
    MsgBox msgall

MsgBox可能太大,具体取决于您的情况。