+ Reply to Thread
Results 1 to 8 of 8

ByRef argument type mismatch / Passing array as a function argument

  1. #1
    Registered User
    Join Date
    01-10-2012
    Location
    Sydney
    MS-Off Ver
    Excel 2007
    Posts
    4

    ByRef argument type mismatch / Passing array as a function argument

    Hi

    The intention is to get the function FD() to accept an integer and an array (of doubles) as input and return a double value. I've searched various forums and references and I *think* I have this part right. However when I compile, I run into a "ByRef argument type mismatch" when calling FD(). I just can't see how the argument types are mismatched, if anyone could help it'd be much appreciated.

    Thanks
    pz

    Please Login or Register  to view this content.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,376

    Re: ByRef argument type mismatch / Passing array as a function argument

    As I understand it, you use ByVal to pass a variable to a function or subroutine when it is needed for the calculation but not changed. You use ByRef if you want to update the variable in the subroutine, that is, you pass its address, not its value.

    You've said that the Function is to return a Variant, not a double. Then you Dim FD as Double in the Function. I would expect the function to be "As Double" and lose the Dim FD As Double.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: ByRef argument type mismatch / Passing array as a function argument

    You've declared inputData as a single variable of type Double, not an array.

    Also, you've declared Close_ in InitialiseDataArray but you then try to refer to it in test.

    You can't do that because it's out of scope.

    Another problem is that you've declared a variable called FD in the function called FD.

    I'm not sure what you are doing with that variable, do you even need it.

    I re-arranged the code a bit but it stops at Highest(CalculationPeriod_) which I assume you haven't written yet.

    Anyway here it is.
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    01-10-2012
    Location
    Sydney
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: ByRef argument type mismatch / Passing array as a function argument

    Thanks guys

    Please see revised code following comments from above.

    One thing that has now popped up is trying to deal with the scope issue of the array Close_. I tried to address it by declaring Close_ in sub test() and passing the array to sub InitialiseDataArray for initialisation, but then I get "Compile Error: Typemismatch: array or user-defined type expected" at
    Please Login or Register  to view this content.
    in sub test()?

    Please Login or Register  to view this content.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: ByRef argument type mismatch / Passing array as a function argument

    Remove the brackets around Close_ when calling InitialiseArray.
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    01-10-2012
    Location
    Sydney
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: ByRef argument type mismatch / Passing array as a function argument

    Ah ha!

    All good now. Thanks all for getting me across the line.

  7. #7
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: ByRef argument type mismatch / Passing array as a function argument

    @ pzling

    Based on your last post it seems that you are satisfied with the solution(s) you've received but you haven't marked your thread as SOLVED. I'll do that for you now but please keep in mind for your future threads that Rule #9 requires you to do that yourself. If your problem has not been solved you can use Thread Tools (located above your first post) and choose "Mark this thread as unsolved".
    Thanks.

    Also, as a relatively new member of the forum, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

  8. #8
    Registered User
    Join Date
    01-10-2012
    Location
    Sydney
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: ByRef argument type mismatch / Passing array as a function argument

    Ah thanks! Noted

+ 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