Is it possible to pass the fields of an array to a function with variant parameter array?










2















I am in the following situation: I have a function, that takes a ParamArray of type variant and generates a string from the keywords given in its ParamArray in a special manner by execution mergeToString.



Function function1(ParamArray var() As Variant) As String
For i = LBound(var) To UBound(var)
function1 = mergeToString(function1, CStr(var(i))
Next i
End Function


In another subroutine, I have an array of strings obtained from the Split Function in VBA and want to use it as an input for function1



Sub displayFCTN1()
Dim arr() As String
arr() = Split("foo|bar", "|")
'and here I ran out of ideas...
Debug.Print function1(**???**)
End Sub


The two lines



function1(**???**)
function1("foo","bar")


should be equivalent the first somehow using arr().



In Matlab this is relatively easy - I know, VBA is not Matlab, still this might help as an extended description of my problem:
you could most likely do it by using the colon operator in Matlab



function1(arr(:))


since then the fields of the array arr() count as "free" parameters.



Is there something comparable to this in VBA? I tried ReDim already, that somehow didn't do the job (as far as I tried).



Thank you for your help!










share|improve this question



















  • 1





    Just a tad bit confused on what exactly you are asking. But just an observation: is mergeToString() essentially the same thing as the built-in Join() function? You've also declared varStr in function1, but it's unused.

    – K.Dᴀᴠɪs
    Nov 16 '18 at 7:09












  • When you pass an array to the paramarray, you get this: var(0)(0) "foo" : function1 = mergeToString(function1, CStr(var(0)(i)) probably will work when you call function1(arr).

    – EvR
    Nov 16 '18 at 8:26











  • @K.Dᴀᴠɪs Sorry for the unused varStr. I corrected it. Concerning the mergeToString() function: In the simplest form as used above, simply concatenates the input strings. It actually offers some more variety when adding more arguments (sorting, layouting,...). If that is, what Join() does, then yes. Sorry, I am a total beginner at VBA..

    – danielphili
    Nov 16 '18 at 11:20











  • @EvR I wouldn't like to change function1 because I would lose compatibility with calls like function1("foo","bar") in this case, which are already in use within the wrapping code. Also, I would prefer a solution like described in Matlab code over an if statement (to query if the variant array is only of length 1 and then convert it to a variant array...) since this is also a bit unflexible.. (I couldn't do function calls like function1(arr(),"abc").

    – danielphili
    Nov 16 '18 at 12:55











  • I don't think VBA can pass array-items directly to ParamArray-items .You could use a helper procedure but that will be maximized to 30 arguments. I'll post this procedure as an answer

    – EvR
    Nov 16 '18 at 14:36
















2















I am in the following situation: I have a function, that takes a ParamArray of type variant and generates a string from the keywords given in its ParamArray in a special manner by execution mergeToString.



Function function1(ParamArray var() As Variant) As String
For i = LBound(var) To UBound(var)
function1 = mergeToString(function1, CStr(var(i))
Next i
End Function


In another subroutine, I have an array of strings obtained from the Split Function in VBA and want to use it as an input for function1



Sub displayFCTN1()
Dim arr() As String
arr() = Split("foo|bar", "|")
'and here I ran out of ideas...
Debug.Print function1(**???**)
End Sub


The two lines



function1(**???**)
function1("foo","bar")


should be equivalent the first somehow using arr().



In Matlab this is relatively easy - I know, VBA is not Matlab, still this might help as an extended description of my problem:
you could most likely do it by using the colon operator in Matlab



function1(arr(:))


since then the fields of the array arr() count as "free" parameters.



Is there something comparable to this in VBA? I tried ReDim already, that somehow didn't do the job (as far as I tried).



Thank you for your help!










share|improve this question



















  • 1





    Just a tad bit confused on what exactly you are asking. But just an observation: is mergeToString() essentially the same thing as the built-in Join() function? You've also declared varStr in function1, but it's unused.

    – K.Dᴀᴠɪs
    Nov 16 '18 at 7:09












  • When you pass an array to the paramarray, you get this: var(0)(0) "foo" : function1 = mergeToString(function1, CStr(var(0)(i)) probably will work when you call function1(arr).

    – EvR
    Nov 16 '18 at 8:26











  • @K.Dᴀᴠɪs Sorry for the unused varStr. I corrected it. Concerning the mergeToString() function: In the simplest form as used above, simply concatenates the input strings. It actually offers some more variety when adding more arguments (sorting, layouting,...). If that is, what Join() does, then yes. Sorry, I am a total beginner at VBA..

    – danielphili
    Nov 16 '18 at 11:20











  • @EvR I wouldn't like to change function1 because I would lose compatibility with calls like function1("foo","bar") in this case, which are already in use within the wrapping code. Also, I would prefer a solution like described in Matlab code over an if statement (to query if the variant array is only of length 1 and then convert it to a variant array...) since this is also a bit unflexible.. (I couldn't do function calls like function1(arr(),"abc").

    – danielphili
    Nov 16 '18 at 12:55











  • I don't think VBA can pass array-items directly to ParamArray-items .You could use a helper procedure but that will be maximized to 30 arguments. I'll post this procedure as an answer

    – EvR
    Nov 16 '18 at 14:36














2












2








2








I am in the following situation: I have a function, that takes a ParamArray of type variant and generates a string from the keywords given in its ParamArray in a special manner by execution mergeToString.



Function function1(ParamArray var() As Variant) As String
For i = LBound(var) To UBound(var)
function1 = mergeToString(function1, CStr(var(i))
Next i
End Function


In another subroutine, I have an array of strings obtained from the Split Function in VBA and want to use it as an input for function1



Sub displayFCTN1()
Dim arr() As String
arr() = Split("foo|bar", "|")
'and here I ran out of ideas...
Debug.Print function1(**???**)
End Sub


The two lines



function1(**???**)
function1("foo","bar")


should be equivalent the first somehow using arr().



In Matlab this is relatively easy - I know, VBA is not Matlab, still this might help as an extended description of my problem:
you could most likely do it by using the colon operator in Matlab



function1(arr(:))


since then the fields of the array arr() count as "free" parameters.



Is there something comparable to this in VBA? I tried ReDim already, that somehow didn't do the job (as far as I tried).



Thank you for your help!










share|improve this question
















I am in the following situation: I have a function, that takes a ParamArray of type variant and generates a string from the keywords given in its ParamArray in a special manner by execution mergeToString.



Function function1(ParamArray var() As Variant) As String
For i = LBound(var) To UBound(var)
function1 = mergeToString(function1, CStr(var(i))
Next i
End Function


In another subroutine, I have an array of strings obtained from the Split Function in VBA and want to use it as an input for function1



Sub displayFCTN1()
Dim arr() As String
arr() = Split("foo|bar", "|")
'and here I ran out of ideas...
Debug.Print function1(**???**)
End Sub


The two lines



function1(**???**)
function1("foo","bar")


should be equivalent the first somehow using arr().



In Matlab this is relatively easy - I know, VBA is not Matlab, still this might help as an extended description of my problem:
you could most likely do it by using the colon operator in Matlab



function1(arr(:))


since then the fields of the array arr() count as "free" parameters.



Is there something comparable to this in VBA? I tried ReDim already, that somehow didn't do the job (as far as I tried).



Thank you for your help!







excel vba excel-vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 12:59







danielphili

















asked Nov 16 '18 at 7:01









danielphilidanielphili

112




112







  • 1





    Just a tad bit confused on what exactly you are asking. But just an observation: is mergeToString() essentially the same thing as the built-in Join() function? You've also declared varStr in function1, but it's unused.

    – K.Dᴀᴠɪs
    Nov 16 '18 at 7:09












  • When you pass an array to the paramarray, you get this: var(0)(0) "foo" : function1 = mergeToString(function1, CStr(var(0)(i)) probably will work when you call function1(arr).

    – EvR
    Nov 16 '18 at 8:26











  • @K.Dᴀᴠɪs Sorry for the unused varStr. I corrected it. Concerning the mergeToString() function: In the simplest form as used above, simply concatenates the input strings. It actually offers some more variety when adding more arguments (sorting, layouting,...). If that is, what Join() does, then yes. Sorry, I am a total beginner at VBA..

    – danielphili
    Nov 16 '18 at 11:20











  • @EvR I wouldn't like to change function1 because I would lose compatibility with calls like function1("foo","bar") in this case, which are already in use within the wrapping code. Also, I would prefer a solution like described in Matlab code over an if statement (to query if the variant array is only of length 1 and then convert it to a variant array...) since this is also a bit unflexible.. (I couldn't do function calls like function1(arr(),"abc").

    – danielphili
    Nov 16 '18 at 12:55











  • I don't think VBA can pass array-items directly to ParamArray-items .You could use a helper procedure but that will be maximized to 30 arguments. I'll post this procedure as an answer

    – EvR
    Nov 16 '18 at 14:36













  • 1





    Just a tad bit confused on what exactly you are asking. But just an observation: is mergeToString() essentially the same thing as the built-in Join() function? You've also declared varStr in function1, but it's unused.

    – K.Dᴀᴠɪs
    Nov 16 '18 at 7:09












  • When you pass an array to the paramarray, you get this: var(0)(0) "foo" : function1 = mergeToString(function1, CStr(var(0)(i)) probably will work when you call function1(arr).

    – EvR
    Nov 16 '18 at 8:26











  • @K.Dᴀᴠɪs Sorry for the unused varStr. I corrected it. Concerning the mergeToString() function: In the simplest form as used above, simply concatenates the input strings. It actually offers some more variety when adding more arguments (sorting, layouting,...). If that is, what Join() does, then yes. Sorry, I am a total beginner at VBA..

    – danielphili
    Nov 16 '18 at 11:20











  • @EvR I wouldn't like to change function1 because I would lose compatibility with calls like function1("foo","bar") in this case, which are already in use within the wrapping code. Also, I would prefer a solution like described in Matlab code over an if statement (to query if the variant array is only of length 1 and then convert it to a variant array...) since this is also a bit unflexible.. (I couldn't do function calls like function1(arr(),"abc").

    – danielphili
    Nov 16 '18 at 12:55











  • I don't think VBA can pass array-items directly to ParamArray-items .You could use a helper procedure but that will be maximized to 30 arguments. I'll post this procedure as an answer

    – EvR
    Nov 16 '18 at 14:36








1




1





Just a tad bit confused on what exactly you are asking. But just an observation: is mergeToString() essentially the same thing as the built-in Join() function? You've also declared varStr in function1, but it's unused.

– K.Dᴀᴠɪs
Nov 16 '18 at 7:09






Just a tad bit confused on what exactly you are asking. But just an observation: is mergeToString() essentially the same thing as the built-in Join() function? You've also declared varStr in function1, but it's unused.

– K.Dᴀᴠɪs
Nov 16 '18 at 7:09














When you pass an array to the paramarray, you get this: var(0)(0) "foo" : function1 = mergeToString(function1, CStr(var(0)(i)) probably will work when you call function1(arr).

– EvR
Nov 16 '18 at 8:26





When you pass an array to the paramarray, you get this: var(0)(0) "foo" : function1 = mergeToString(function1, CStr(var(0)(i)) probably will work when you call function1(arr).

– EvR
Nov 16 '18 at 8:26













@K.Dᴀᴠɪs Sorry for the unused varStr. I corrected it. Concerning the mergeToString() function: In the simplest form as used above, simply concatenates the input strings. It actually offers some more variety when adding more arguments (sorting, layouting,...). If that is, what Join() does, then yes. Sorry, I am a total beginner at VBA..

– danielphili
Nov 16 '18 at 11:20





@K.Dᴀᴠɪs Sorry for the unused varStr. I corrected it. Concerning the mergeToString() function: In the simplest form as used above, simply concatenates the input strings. It actually offers some more variety when adding more arguments (sorting, layouting,...). If that is, what Join() does, then yes. Sorry, I am a total beginner at VBA..

– danielphili
Nov 16 '18 at 11:20













@EvR I wouldn't like to change function1 because I would lose compatibility with calls like function1("foo","bar") in this case, which are already in use within the wrapping code. Also, I would prefer a solution like described in Matlab code over an if statement (to query if the variant array is only of length 1 and then convert it to a variant array...) since this is also a bit unflexible.. (I couldn't do function calls like function1(arr(),"abc").

– danielphili
Nov 16 '18 at 12:55





@EvR I wouldn't like to change function1 because I would lose compatibility with calls like function1("foo","bar") in this case, which are already in use within the wrapping code. Also, I would prefer a solution like described in Matlab code over an if statement (to query if the variant array is only of length 1 and then convert it to a variant array...) since this is also a bit unflexible.. (I couldn't do function calls like function1(arr(),"abc").

– danielphili
Nov 16 '18 at 12:55













I don't think VBA can pass array-items directly to ParamArray-items .You could use a helper procedure but that will be maximized to 30 arguments. I'll post this procedure as an answer

– EvR
Nov 16 '18 at 14:36






I don't think VBA can pass array-items directly to ParamArray-items .You could use a helper procedure but that will be maximized to 30 arguments. I'll post this procedure as an answer

– EvR
Nov 16 '18 at 14:36













3 Answers
3






active

oldest

votes


















0














A workaround as mentioned in the comments above



Sub displayFCTN1()
Dim arr() As String
arr() = Split("foo|bar", "|")
Myhelper arr
End Sub

Sub Myhelper(arr)
Select Case UBound(arr)
Case 0: Debug.Print function1(arr(0))
Case 1: Debug.Print function1(arr(0), arr(1))
Case 2: Debug.Print function1(arr(0), arr(1), arr(2))
Case 3: Debug.Print function1(arr(0), arr(1), arr(2), arr(3))
Case 4: Debug.Print function1(arr(0), arr(1), arr(2), arr(3), arr(4))
'etc up to 29.
Case Else
End Select
End Sub





share|improve this answer






























    0














    This does require a change to function1 code, but should still work with orginal.



    Sub Test()

    Debug.Print function1("foo", "bar")

    Dim arr() As String
    arr = Split("foo|bar", "|")
    Debug.Print function1(arr)

    End Sub

    Function function1(ParamArray var() As Variant) As String
    Dim i As Long

    If UBound(var) = 0 Then
    For i = LBound(var(0)) To UBound(var(0))
    'function1 = Join(var(0), "|")
    function1 = mergeToString(function1, CStr(var(0)(i)))
    Next i
    Else
    'Original code.
    For i = LBound(var) To UBound(var)
    'function1 = Join(var, "|")
    function1 = mergeToString(function1, CStr(var(i)))
    Next i
    End If
    End Function





    share|improve this answer






























      0














      You need to test, whether the first item of array is array:



      Sub FFF()
      MsgBox Func1("foo", "bar")
      MsgBox Func1(Split("foo|bar", "|"))
      End Sub

      Function Func1$(ParamArray var() As Variant)
      Dim s$, x%, args
      args = IIf(IsArray(var(0)), var(0), var)
      '//Do something
      For x = 0 To UBound(args)
      s = s & args(x) & "|"
      Next
      Func1 = Left$(s, Len(s) - 1)
      End Function





      share|improve this answer























      • This will immediately cause problems as soon as you want to do something like function1("abc", arr()) since it is designed to check the first element of the parameter array only, right? Sorry, if the need for this kind of flexibility was not clear so far...

        – danielphili
        Nov 18 '18 at 8:50











      • @danielphili Then test every parameter for being array...

        – JohnyL
        Nov 18 '18 at 9:18











      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332922%2fis-it-possible-to-pass-the-fields-of-an-array-to-a-function-with-variant-paramet%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      A workaround as mentioned in the comments above



      Sub displayFCTN1()
      Dim arr() As String
      arr() = Split("foo|bar", "|")
      Myhelper arr
      End Sub

      Sub Myhelper(arr)
      Select Case UBound(arr)
      Case 0: Debug.Print function1(arr(0))
      Case 1: Debug.Print function1(arr(0), arr(1))
      Case 2: Debug.Print function1(arr(0), arr(1), arr(2))
      Case 3: Debug.Print function1(arr(0), arr(1), arr(2), arr(3))
      Case 4: Debug.Print function1(arr(0), arr(1), arr(2), arr(3), arr(4))
      'etc up to 29.
      Case Else
      End Select
      End Sub





      share|improve this answer



























        0














        A workaround as mentioned in the comments above



        Sub displayFCTN1()
        Dim arr() As String
        arr() = Split("foo|bar", "|")
        Myhelper arr
        End Sub

        Sub Myhelper(arr)
        Select Case UBound(arr)
        Case 0: Debug.Print function1(arr(0))
        Case 1: Debug.Print function1(arr(0), arr(1))
        Case 2: Debug.Print function1(arr(0), arr(1), arr(2))
        Case 3: Debug.Print function1(arr(0), arr(1), arr(2), arr(3))
        Case 4: Debug.Print function1(arr(0), arr(1), arr(2), arr(3), arr(4))
        'etc up to 29.
        Case Else
        End Select
        End Sub





        share|improve this answer

























          0












          0








          0







          A workaround as mentioned in the comments above



          Sub displayFCTN1()
          Dim arr() As String
          arr() = Split("foo|bar", "|")
          Myhelper arr
          End Sub

          Sub Myhelper(arr)
          Select Case UBound(arr)
          Case 0: Debug.Print function1(arr(0))
          Case 1: Debug.Print function1(arr(0), arr(1))
          Case 2: Debug.Print function1(arr(0), arr(1), arr(2))
          Case 3: Debug.Print function1(arr(0), arr(1), arr(2), arr(3))
          Case 4: Debug.Print function1(arr(0), arr(1), arr(2), arr(3), arr(4))
          'etc up to 29.
          Case Else
          End Select
          End Sub





          share|improve this answer













          A workaround as mentioned in the comments above



          Sub displayFCTN1()
          Dim arr() As String
          arr() = Split("foo|bar", "|")
          Myhelper arr
          End Sub

          Sub Myhelper(arr)
          Select Case UBound(arr)
          Case 0: Debug.Print function1(arr(0))
          Case 1: Debug.Print function1(arr(0), arr(1))
          Case 2: Debug.Print function1(arr(0), arr(1), arr(2))
          Case 3: Debug.Print function1(arr(0), arr(1), arr(2), arr(3))
          Case 4: Debug.Print function1(arr(0), arr(1), arr(2), arr(3), arr(4))
          'etc up to 29.
          Case Else
          End Select
          End Sub






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 14:38









          EvREvR

          1,2362414




          1,2362414























              0














              This does require a change to function1 code, but should still work with orginal.



              Sub Test()

              Debug.Print function1("foo", "bar")

              Dim arr() As String
              arr = Split("foo|bar", "|")
              Debug.Print function1(arr)

              End Sub

              Function function1(ParamArray var() As Variant) As String
              Dim i As Long

              If UBound(var) = 0 Then
              For i = LBound(var(0)) To UBound(var(0))
              'function1 = Join(var(0), "|")
              function1 = mergeToString(function1, CStr(var(0)(i)))
              Next i
              Else
              'Original code.
              For i = LBound(var) To UBound(var)
              'function1 = Join(var, "|")
              function1 = mergeToString(function1, CStr(var(i)))
              Next i
              End If
              End Function





              share|improve this answer



























                0














                This does require a change to function1 code, but should still work with orginal.



                Sub Test()

                Debug.Print function1("foo", "bar")

                Dim arr() As String
                arr = Split("foo|bar", "|")
                Debug.Print function1(arr)

                End Sub

                Function function1(ParamArray var() As Variant) As String
                Dim i As Long

                If UBound(var) = 0 Then
                For i = LBound(var(0)) To UBound(var(0))
                'function1 = Join(var(0), "|")
                function1 = mergeToString(function1, CStr(var(0)(i)))
                Next i
                Else
                'Original code.
                For i = LBound(var) To UBound(var)
                'function1 = Join(var, "|")
                function1 = mergeToString(function1, CStr(var(i)))
                Next i
                End If
                End Function





                share|improve this answer

























                  0












                  0








                  0







                  This does require a change to function1 code, but should still work with orginal.



                  Sub Test()

                  Debug.Print function1("foo", "bar")

                  Dim arr() As String
                  arr = Split("foo|bar", "|")
                  Debug.Print function1(arr)

                  End Sub

                  Function function1(ParamArray var() As Variant) As String
                  Dim i As Long

                  If UBound(var) = 0 Then
                  For i = LBound(var(0)) To UBound(var(0))
                  'function1 = Join(var(0), "|")
                  function1 = mergeToString(function1, CStr(var(0)(i)))
                  Next i
                  Else
                  'Original code.
                  For i = LBound(var) To UBound(var)
                  'function1 = Join(var, "|")
                  function1 = mergeToString(function1, CStr(var(i)))
                  Next i
                  End If
                  End Function





                  share|improve this answer













                  This does require a change to function1 code, but should still work with orginal.



                  Sub Test()

                  Debug.Print function1("foo", "bar")

                  Dim arr() As String
                  arr = Split("foo|bar", "|")
                  Debug.Print function1(arr)

                  End Sub

                  Function function1(ParamArray var() As Variant) As String
                  Dim i As Long

                  If UBound(var) = 0 Then
                  For i = LBound(var(0)) To UBound(var(0))
                  'function1 = Join(var(0), "|")
                  function1 = mergeToString(function1, CStr(var(0)(i)))
                  Next i
                  Else
                  'Original code.
                  For i = LBound(var) To UBound(var)
                  'function1 = Join(var, "|")
                  function1 = mergeToString(function1, CStr(var(i)))
                  Next i
                  End If
                  End Function






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 15:09









                  Darren Bartrup-CookDarren Bartrup-Cook

                  14k11533




                  14k11533





















                      0














                      You need to test, whether the first item of array is array:



                      Sub FFF()
                      MsgBox Func1("foo", "bar")
                      MsgBox Func1(Split("foo|bar", "|"))
                      End Sub

                      Function Func1$(ParamArray var() As Variant)
                      Dim s$, x%, args
                      args = IIf(IsArray(var(0)), var(0), var)
                      '//Do something
                      For x = 0 To UBound(args)
                      s = s & args(x) & "|"
                      Next
                      Func1 = Left$(s, Len(s) - 1)
                      End Function





                      share|improve this answer























                      • This will immediately cause problems as soon as you want to do something like function1("abc", arr()) since it is designed to check the first element of the parameter array only, right? Sorry, if the need for this kind of flexibility was not clear so far...

                        – danielphili
                        Nov 18 '18 at 8:50











                      • @danielphili Then test every parameter for being array...

                        – JohnyL
                        Nov 18 '18 at 9:18















                      0














                      You need to test, whether the first item of array is array:



                      Sub FFF()
                      MsgBox Func1("foo", "bar")
                      MsgBox Func1(Split("foo|bar", "|"))
                      End Sub

                      Function Func1$(ParamArray var() As Variant)
                      Dim s$, x%, args
                      args = IIf(IsArray(var(0)), var(0), var)
                      '//Do something
                      For x = 0 To UBound(args)
                      s = s & args(x) & "|"
                      Next
                      Func1 = Left$(s, Len(s) - 1)
                      End Function





                      share|improve this answer























                      • This will immediately cause problems as soon as you want to do something like function1("abc", arr()) since it is designed to check the first element of the parameter array only, right? Sorry, if the need for this kind of flexibility was not clear so far...

                        – danielphili
                        Nov 18 '18 at 8:50











                      • @danielphili Then test every parameter for being array...

                        – JohnyL
                        Nov 18 '18 at 9:18













                      0












                      0








                      0







                      You need to test, whether the first item of array is array:



                      Sub FFF()
                      MsgBox Func1("foo", "bar")
                      MsgBox Func1(Split("foo|bar", "|"))
                      End Sub

                      Function Func1$(ParamArray var() As Variant)
                      Dim s$, x%, args
                      args = IIf(IsArray(var(0)), var(0), var)
                      '//Do something
                      For x = 0 To UBound(args)
                      s = s & args(x) & "|"
                      Next
                      Func1 = Left$(s, Len(s) - 1)
                      End Function





                      share|improve this answer













                      You need to test, whether the first item of array is array:



                      Sub FFF()
                      MsgBox Func1("foo", "bar")
                      MsgBox Func1(Split("foo|bar", "|"))
                      End Sub

                      Function Func1$(ParamArray var() As Variant)
                      Dim s$, x%, args
                      args = IIf(IsArray(var(0)), var(0), var)
                      '//Do something
                      For x = 0 To UBound(args)
                      s = s & args(x) & "|"
                      Next
                      Func1 = Left$(s, Len(s) - 1)
                      End Function






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 16 '18 at 20:03









                      JohnyLJohnyL

                      3,72811025




                      3,72811025












                      • This will immediately cause problems as soon as you want to do something like function1("abc", arr()) since it is designed to check the first element of the parameter array only, right? Sorry, if the need for this kind of flexibility was not clear so far...

                        – danielphili
                        Nov 18 '18 at 8:50











                      • @danielphili Then test every parameter for being array...

                        – JohnyL
                        Nov 18 '18 at 9:18

















                      • This will immediately cause problems as soon as you want to do something like function1("abc", arr()) since it is designed to check the first element of the parameter array only, right? Sorry, if the need for this kind of flexibility was not clear so far...

                        – danielphili
                        Nov 18 '18 at 8:50











                      • @danielphili Then test every parameter for being array...

                        – JohnyL
                        Nov 18 '18 at 9:18
















                      This will immediately cause problems as soon as you want to do something like function1("abc", arr()) since it is designed to check the first element of the parameter array only, right? Sorry, if the need for this kind of flexibility was not clear so far...

                      – danielphili
                      Nov 18 '18 at 8:50





                      This will immediately cause problems as soon as you want to do something like function1("abc", arr()) since it is designed to check the first element of the parameter array only, right? Sorry, if the need for this kind of flexibility was not clear so far...

                      – danielphili
                      Nov 18 '18 at 8:50













                      @danielphili Then test every parameter for being array...

                      – JohnyL
                      Nov 18 '18 at 9:18





                      @danielphili Then test every parameter for being array...

                      – JohnyL
                      Nov 18 '18 at 9:18

















                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332922%2fis-it-possible-to-pass-the-fields-of-an-array-to-a-function-with-variant-paramet%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      政党

                      天津地下鉄3号線