+ Reply to Thread
Results 1 to 4 of 4

passing multiple strings to a function

  1. #1
    Tijmen
    Guest

    passing multiple strings to a function

    It seems that it is not possible to pass multiple string arguments to a
    function in vba.
    I am using vba in Excel 2000.
    Why does this not work?

    Dim testStr1
    Dim testStr2

    testStr1 = "hi"
    testStr2 = "there"

    ' I get a compile error on the next statement
    TestSub(testStr1, testStr2)


    Sub TestSub(str1 as String, str2 as String)

    MsgBox(str1 & str2)

    End sub



    Thanks,

    Tijmen



  2. #2
    quartz
    Guest

    RE: passing multiple strings to a function

    I would specify the variable types like so:

    Dim testStr1 As String
    Dim testStr2 As String

    Then, you must use the "Call" key word when referencing a function with
    arguments like so:

    Call TestSub(testStr1, testStr2)

    HTH.

    "Tijmen" wrote:

    > It seems that it is not possible to pass multiple string arguments to a
    > function in vba.
    > I am using vba in Excel 2000.
    > Why does this not work?
    >
    > Dim testStr1
    > Dim testStr2
    >
    > testStr1 = "hi"
    > testStr2 = "there"
    >
    > ' I get a compile error on the next statement
    > TestSub(testStr1, testStr2)
    >
    >
    > Sub TestSub(str1 as String, str2 as String)
    >
    > MsgBox(str1 & str2)
    >
    > End sub
    >
    >
    >
    > Thanks,
    >
    > Tijmen
    >
    >
    >


  3. #3
    Dave Peterson
    Guest

    Re: passing multiple strings to a function

    This worked ok for me:

    Option Explicit
    Sub test1()
    Dim testStr1 As String
    Dim testStr2 As String
    testStr1 = "hi"
    testStr2 = "there"
    TestSub testStr1, testStr2
    End Sub
    Sub TestSub(str1 As String, str2 As String)
    MsgBox (str1 & str2)
    End Sub


    TestSub was expecting strings.
    You were passing two variants
    Dim testStr1
    Dim testStr2
    is the equivalent of:
    Dim testStr1 as variant
    Dim testStr2 as variant

    I could have also used:
    TestSub cstr(testStr1), cstr(testStr2)
    and kept the variables as variants.



    Tijmen wrote:
    >
    > It seems that it is not possible to pass multiple string arguments to a
    > function in vba.
    > I am using vba in Excel 2000.
    > Why does this not work?
    >
    > Dim testStr1
    > Dim testStr2
    >
    > testStr1 = "hi"
    > testStr2 = "there"
    >
    > ' I get a compile error on the next statement
    > TestSub(testStr1, testStr2)
    >
    > Sub TestSub(str1 as String, str2 as String)
    >
    > MsgBox(str1 & str2)
    >
    > End sub
    >
    > Thanks,
    >
    > Tijmen


    --

    Dave Peterson

  4. #4
    Andrew Taylor
    Guest

    Re: passing multiple strings to a function

    Subs are called without enclosing the parameter list in parentheses
    (which is probably what the error message was telling you), unless
    you use the Call statement, so you need either:

    TestSub testStr1, testStr2

    or

    Call TestSub(testStr1, testStr2)

    Andrew Taylor


    Tijmen wrote:
    > It seems that it is not possible to pass multiple string arguments to a
    > function in vba.
    > I am using vba in Excel 2000.
    > Why does this not work?
    >
    > Dim testStr1
    > Dim testStr2
    >
    > testStr1 = "hi"
    > testStr2 = "there"
    >
    > ' I get a compile error on the next statement
    > TestSub(testStr1, testStr2)
    >
    >
    > Sub TestSub(str1 as String, str2 as String)
    >
    > MsgBox(str1 & str2)
    >
    > End sub
    >
    >
    >
    > Thanks,
    >
    > Tijmen



+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1