+ Reply to Thread
Results 1 to 3 of 3

Passing array to procedure gives type mismatch error

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Passing array to procedure gives type mismatch error

    Hi,

    I have run into this scenario before and in the past i have always fiddled around with the code to get my code to run without really ever "learning" why this problem occurs. I have read a few links and passages in books on arrays and passing arrays and have not really found my answer so i am going to post my issue here and hope someone can explain it to me.

    Consider the following code:
    Please Login or Register  to view this content.
    if i try to run the above code i get a type mismatch error. However, if i change the declaration for temp_array1 from

    Please Login or Register  to view this content.
    to

    Please Login or Register  to view this content.
    It runs fine..........



    Can someone explain why?

    Also, why does having "()" around declaration in procedure "pass_array_procedure2" cause an error?

    I just never know when to include "()" or how to declare my variant-arrays in the main procedure.

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Passing array to procedure gives type mismatch error

    pass_array_procedure2 is explicitly stating that it will only accept an array. At compile time the application doesn't think that temp_array1 is an array so you get a type mismatch error.

    If you change:
    Please Login or Register  to view this content.
    to

    Please Login or Register  to view this content.
    Both will work as you've told the compiler that temp_array1 is an array

  3. #3
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Passing array to procedure gives type mismatch error

    With your original code, temp_array1 is a Variant, not an array, which your second procedure requires. The fact that temp_Array1 may happen to contain an array does not alter that.

    Declaring the variable as an array solves that issue - the first procedure will still accept it since an array is always Let coercible to a Variant (i.e. a Variant can contain anything, including an array).
    Last edited by romperstomper; 01-14-2015 at 08:15 AM.
    Remember what the dormouse said
    Feed your head

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Runtime error caused by passing array to procedure
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-26-2014, 08:13 PM
  2. [SOLVED] Fixing Type Mismatch Error in argument passing
    By josel2820 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-22-2013, 05:38 PM
  3. Error passing element of string array get ByRef argument type mismatch
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-17-2011, 02:59 PM
  4. [SOLVED] Help: Compile error: type mismatch: array or user defined type expected
    By lvcha.gouqizi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-31-2005, 05:05 PM
  5. Passing Userform Listbox to sub causes type-mismatch error
    By Kleev in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-19-2005, 04:05 PM

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