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

如何将所有消息框添加到VBA中的最终消息框中?,vba,excel,Vba,Excel,谁能帮帮我们吗。我们已设法让VBA查看我们的数据,并找到已开放超过90天的门票。但目前我们只能让每张票都显示为一个单独的信息框。是否可以将最后的所有结果合并到一个包含结果列表的消息框中,并计算找到的结果总数?下面是我们正在使用的代码 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

谁能帮帮我们吗。我们已设法让VBA查看我们的数据,并找到已开放超过90天的门票。但目前我们只能让每张票都显示为一个单独的信息框。是否可以将最后的所有结果合并到一个包含结果列表的消息框中,并计算找到的结果总数?下面是我们正在使用的代码

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
Sub-longerthan 3个月()
最后一排一样长
我想我会坚持多久
Dim startdate作为日期
Dim datenow As Date
作为字符串的模糊票据
以整数表示的暗淡天数
作为整数的Dim n
表格(“票证”)。选择
使用ActiveSheet
lastrow=.Cells(.Rows.Count,“B”).End(xlUp).Row
以
datenow=日期
n=0
对于i=4到最后一行
起始日期=范围(“B”和i)。值
天=日期差(“d”,开始日期,现在)
如果天数>=90,则
如果范围(“B”&i).偏移量(,2).值“关闭”,则
n=n+1
票证=范围(“B”和i).偏移量(,3).值
如果结束
如果结束
接下来我
MsgBox(n&“和”票证已开放超过90天)
端接头

您需要将Results连接成一个字符串

在循环之外,您应该获得一个字符串变量

  Dim msgStr as String
  Dim extraTickets as Long

  extraTickets = 0
然后每次将
票证
字段的串联添加到字符串中

   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
试试这个

    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
Dim msgall作为字符串
msgall=“”
...
对于i=4到最后一行
...
如果范围(“B”&i).偏移量(,2).值“关闭”,则
N=N+1
票证=范围(“B”和i).偏移量(,3).值
msgall=msgall&&vbCrLf
如果结束
...
接下来我
...
MsgBox-msgall

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

请告诉我。但是值得注意的是,
MsgBox
只能显示有限数量的信息(不确定确切的字符/行数)。当我使用类似的方法显示Access数据库的更新信息时发现,过多的票证和以后的票证将被裁剪掉。仍然得到我的+1。@Aiken这是一个很好的观点。OP可能需要在n个结果后进行逻辑检查以停止连接。谢谢你们的回答,它工作得非常好。如果reults的长度最终超过了一个消息框的长度,你建议我怎么做?再次感谢你help@MichaelAsaad如果计算出列表中的票证数量合理,则可以在连接上设置条件。我将编辑答案。列表中只有10个答案,但您将无法滚动。我们在这里进入了一个扩展的评论行,这是不鼓励的,我会在
msgbox
上做一些研究,然后发布另一个问题,如果你有问题的话。
    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