+ Reply to Thread
Results 1 to 5 of 5

Syntax for Passing Arrays to Functions

Hybrid View

  1. #1
    Valued Forum Contributor ffffloyd's Avatar
    Join Date
    05-20-2008
    Location
    Perth, WA, Australia
    MS-Off Ver
    Office 365
    Posts
    249

    Syntax for Passing Arrays to Functions

    What is the syntax for passing arrays to functions?

    In other words, for the following simple test example, what would work? It is highlighting the asValue parameter and saying, "Type mismatch: array or user-defined type expected". This occurs whether or not I follow it with the empty parentheses.
    Function ArrayCall(ByRef A() As Variant) As Boolean
    
        ArrayCall = True
        
    End Function
    
    Sub TestArrayCall()
    Dim asValue(2) As String
    
        Call ArrayCall(asValue())
        
    End Sub
    I want the function to be able to accept an array of any type, hence the "As Variant" type in the definition despite the call being made with an array of strings.
    Last edited by ffffloyd; 04-21-2009 at 08:34 PM.

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Syntax for Passing Arrays to Functions

    I would suggest a read through of Chip Pearson's page: http://www.cpearson.com/excel/Passin...ningArrays.htm

    If you want to accept Variants you must pass Variants.

  3. #3
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264

    Re: Syntax for Passing Arrays to Functions

    Nice page reference Luke.

    In other words change your function declaration to:

    Function ArrayCall(ByRef A As Variant) As Boolean
    Richard
    Richard Schollar
    Microsoft MVP - Excel

  4. #4
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Syntax for Passing Arrays to Functions

    Quote Originally Posted by RichardSchollar View Post
    Nice page reference Luke.

    In other words change your function declaration to:

    Function ArrayCall(ByRef A As Variant) As Boolean
    Richard
    mis read message
    Last edited by foxguy; 04-21-2009 at 12:55 PM. Reason: Misread

  5. #5
    Valued Forum Contributor ffffloyd's Avatar
    Join Date
    05-20-2008
    Location
    Perth, WA, Australia
    MS-Off Ver
    Office 365
    Posts
    249

    Re: Syntax for Passing Arrays to Functions

    Thanks, Richard. That's what I needed to know.

+ 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