首页 > 解决方案 > 循环显示多条记录的消息框

问题描述

我正在尝试创建一个消息框,显示未确定数量的列的各种条目。

我相信我想要一个循环,但我希望消息框显示一个范围内的所有可用记录,而不是为范围内的每个项目创建一个新的消息框。

我希望消息框看起来像

Loan Summary(Price, Range, Standard Deviation):
Loan 1: (100, 5, 2)
Loan 2: (102, 4, 3)

依此类推,但记录(贷款)的数量每次都会改变。

我有下面的代码。如何为范围内的每条记录添加新行?

For theRep = 1 To wsv.Range("J3").Value
    Average1 = Range("loanSummary").Offset(0, theRep)
    Range1 = Range("loanSummary").Offset(1, theRep)
    StdDev1 = Range("loanSummary").Offset(2, theRep)

    MsgBox "Loan Summary (Price, Range, Standard Deviation):" & vbCrLf & vbTab & "Loan 1: " & Format(Average1, "##0.00") & ", " & Format(Range1, "##0.00") & ", " & Format(StdDev1, "##0.00")
Next

标签: excelvba

解决方案


使用字符串变量来保存数据,然后在循环之后将字符串显示在一个 MsgBox 中

Dim str As String
str = "Loan Summary (Price, Range, Standard Deviation):" & vbCrLf & vbTab
For theRep = 1 To wsv.Range("J3").Value
    Average1 = Range("loanSummary").Offset(0, theRep)
    Range1 = Range("loanSummary").Offset(1, theRep)
    StdDev1 = Range("loanSummary").Offset(2, theRep)


    str = str & "Loan " & theRep & ": (" & Format(Average1, "##0.00") & ", " & Format(Range1, "##0.00") & ", " & Format(StdDev1, "##0.00") & ")" & vbCrLf & vbTab
Next
MsgBox str

推荐阅读