0

The code below is a fragment of a macro I'm writing to alert users that they may not have a sufficient number of compatible installation equipment on an order. n shortfalls (i.e., each instance of an equipment item not having compatible installation equipment are saved in the yankees() array in elements 1 to n. What I want to do is prompt users with a message box stating "Please review your order to ensure you have you sufficient compatible installation equipment- we detected the following shortfalls"

and below that

include all each element of yankees(1 to n) on separate lines in a message box with two options below "This is okay, I'll submit my order now" and "Let me go back,I want to modify my order".

How can I create such a message box?

I have:

 MsgBox "Please review your order to ensure you have you sufficient compatible     installation equipment- we detected the following concerns" & yankee(1), vbOKCancel

currently but this only includes the first shortfall. How can I include all elements of yankee() and put them on their own line?

This question really boils down to: "How do I put all non-blank elements of an array variable onto their own lines in a message box prompt"?

Do
If rip(qbert) < k(qbert) Then
yankee(jets) = "Your order for" & s(qbert) & " contains " & k(qbert) - rip(qbert) & " too     few " & g(qbert)
jets = jets + 1
qbert = qbert + 1
Else
qbert = qbert + 1
End If
Loop Until qbert > echo
4

2 回答 2

5

您可以使用以下Join功能:

Sub Test()
    Dim var As Variant

    'Populate a dummy vector array from a comma-separated list:
    var = Split("Alpha,Beta,Gamma,Delta,Epsilon", ",")

    'Display the contents of the array as delimited list, use the Carriage Return to delimit:
    MsgBox Join(var, vbCR)

End Sub

在此处输入图像描述

以上不忽略空格。要忽略空白,根据您的具体问题,您可以遍历数组并测试空白值。我会这样做Function

如何在消息框提示中将数组变量的所有非空白元素放到它们自己的行中

在您的子程序中,只需传递yankees给此函数,例如:

MsgBox = GetMessageText(yankees)

这是功能:

Function GetMessageText(var As Variant) As String
    'Assumes a vector array
    On Error GoTo EarlyExit

    Dim sMsg As String
    Dim v As Variant

    For Each v In var
        If Not v = vbNullString Then
            sMsg = sMsg & v & vbCr
        End If
    Next

EarlyExit:
    If Err.Number = 0 Then
        GetMessageText = sMsg
    Else:
        GetMessageText = "invalid array"
    End If

End Function
于 2013-09-03T16:09:44.613 回答
2

Alternate:

Sub tgr()

    Dim yankee(1 To 5) As String
    Dim strMsg As String

    yankee(1) = "Did this"
    yankee(3) = "experiment"
    yankee(5) = "really work?"

    'yankee => ["Did this", , "experiment", , "really work?"] _
     the yankee array has two blanks at positions 2 and 4 _
     and it also has spaces in some of the element strings

    strMsg = Replace(Replace(WorksheetFunction.Trim(Replace(Replace(Join(yankee, "|"), " ", "_"), "|", " ")), " ", vbCrLf), "_", " ")

    'strMsg => "Did this _
                experiment _
                really work?"

    'Yes it did, see result
    MsgBox strMsg

End Sub
于 2013-09-03T16:53:51.997 回答