문제

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
도움이 되었습니까?

해결책

You can use the Join function:

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

enter image description here

The above does not ignore blanks. To ignore blanks, per your specific question, you can iterate over the array and test for blank values. I would do this in a Function:

How do I put all non-blank elements of an array variable onto their own lines in a message box prompt

In your sub, just pass yankees to this function, like:

MsgBox = GetMessageText(yankees)

Here is the function:

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

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top