+ Reply to Thread
Results 1 to 7 of 7

Error passing element of string array get ByRef argument type mismatch

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

    Error passing element of string array get ByRef argument type mismatch

    I have an array that is defined as follows:

    Please Login or Register  to view this content.

    I assign values to the array like this


    Please Login or Register  to view this content.

    I then try to pass the first element of the array and thats when iget the ByRef error.

    Please Login or Register  to view this content.
    any ideas what i am doing wrong?

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Error passing element of string array get ByRef argument type mismatch

    welchs101,

    Is the code for create_header_row_new_combined_file expecting a string or worksheet variable as its second argument?
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

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

    Re: Error passing element of string array get ByRef argument type mismatch

    right now it is setup to expect a string variable

  4. #4
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Error passing element of string array get ByRef argument type mismatch

    welchs101,

    Try using this for the Call line. It uses the StrConv method to ensure that sheet_array(1) is being passed as a string argument:
    Please Login or Register  to view this content.

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

    Re: Error passing element of string array get ByRef argument type mismatch

    that worked...........but i wonder why my original code did not work.........it should have ......right?

    thanks again.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Error passing element of string array get ByRef argument type mismatch

    Hello welchs101,

    This gets into the world of ByRef and ByVal arguments. When an argument is passed ByRef (by reference) the address of the argument is passed. For this to happen, the argument must have been declared by either by a Dim statement or the procedure being called.

    ByVal means only the value of the argument is being passed not the address pointing to it. The Call statement examines the argument list presented and decides if the argument is to be passed by value or by reference. If you wish, you can explicitly state how you want to pass the argument in the argument list by preceding the argument with ByRef or ByVal.

    You defined the variable "sheet_array" as an Array which can be passed by reference but the individual elements have not. Since they have not been assigned, they must be passed by value. The Call statement figures that piece out for you.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  7. #7
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,252

    Re: Error passing element of string array get ByRef argument type mismatch

    It is because each element in sheet_array() is a Variant type, but your create_header_row_new_combined_file() sub expects to be passed a String type. You can either explicitly cast to a String (recommended approach for which I would use CStr()), or you can force it to be passed ByVal by surrounding it with brackets.

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Alternatively, of course, you could change the parameter declaration in create_header_row_new_combined_file so it expects aVariant or the argument to be passed ByVal, or you could change the sheet_array() declaration so it is a String array and populate it using Split() rather than Array().
    Last edited by Colin Legg; 10-17-2011 at 03:08 PM.
    Hope that helps,

    Colin

    RAD Excel Blog

+ 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