Finance

Charts

Statistics

Macros

Search

Using an Indefinite Number of Parameters with Excel VBA

As a rule, the number of arguments passed to a procedure matches the number of parameters defined for that procedure. However, the keyword ParamArray allows you to pass an arbitrary, unspecified number of arguments to a procedure (similar to how the worksheet function SUM() in MS Excel works).

As an example, consider the following Function procedure, which concatenates all strings passed as parameters into a single string.

Example of a procedure with an indefinite number of parameters

Function PutTogether(FirstWord As String, _
                     ParamArray Words() As Variant) As String
    Dim s As String
    Dim a As Variant
    s = FirstWord
    For Each a In Words
        s = s & a
    Next a
    PutTogether = s
End Function

Sub DemoPutTogether()
    Dim a As String, b As String, c As String
    a = "Drop " : b = "wears " : c = "away stone"
    MsgBox PutTogether("Proverb: ", a, b, c)
    ' Displays: Proverb: Drop wears away stone
    MsgBox PutTogether("Saying: ", "Perseverance ", "and hard work", _
                       " overcome ", "everything")
    ' Displays: Saying: Perseverance and hard work overcome everything
End Sub
0 0 votes
Évaluation de l'article
S’abonner
Notification pour
guest
0 Commentaires
Le plus ancien
Le plus récent Le plus populaire
Online comments
Show all comments
Facebook
Twitter
LinkedIn
WhatsApp
Email
Print
0
We’d love to hear your thoughts — please leave a commentx