+ Reply to Thread
Results 1 to 8 of 8

Worksheet function with variable number of parameters

  1. #1
    Forum Contributor
    Join Date
    05-30-2013
    Location
    France
    MS-Off Ver
    2010
    Posts
    260

    Worksheet function with variable number of parameters

    Hi,

    I'm trying to write a sub to create a worksheetfunction that would be called with an array.
    It could be called with ["B","D","P"] or ["F","H","Q",T"] etc. The goal would be to have a formula like so =SUM(B3,D3,P3) in R3 or =SUM(F3,H3,Q3,T3) in V3. If easier I could also call the sub with [2,4,16] and use the R1C1 notation style. I've seen some potential solutions with ParamArray.

    Please Login or Register  to view this content.
    Any ideas ?

    Thanks,
    amphi
    Last edited by amphinomos; 03-14-2016 at 10:02 AM.

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Worksheet function with variable number of parameters

    Use in cell like

    =MySum(3,"b","p","m")

    Please Login or Register  to view this content.

  3. #3
    Forum Contributor
    Join Date
    05-30-2013
    Location
    France
    MS-Off Ver
    2010
    Posts
    260

    Re: Worksheet function with variable number of parameters

    Thanks. Calling the function but I'm getting a type mismatch error on the MySum line but if I set e to string then I can't loop the array...
    Please Login or Register  to view this content.
    Last edited by amphinomos; 03-14-2016 at 10:23 AM.

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Worksheet function with variable number of parameters

    That will be different.
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    05-30-2013
    Location
    France
    MS-Off Ver
    2010
    Posts
    260

    Re: Worksheet function with variable number of parameters

    I get a type mismatch when checking for TypeName. Is something wrong with my array ?

    Please Login or Register  to view this content.

  6. #6
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Worksheet function with variable number of parameters

    Your Totals is an Error value, hense the type mismatch

    One technique for using a ParmaArray is to test the first argument of that array. If that element is an array, use just that element, if not then use the whole ParamArray


    Please Login or Register  to view this content.
    You would call it in one of these manners.
    Please Login or Register  to view this content.
    Note that the array Totals does not need to be 0 based.
    Last edited by mikerickson; 03-14-2016 at 10:53 AM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Worksheet function with variable number of parameters

    See my previous post.

  8. #8
    Forum Contributor
    Join Date
    05-30-2013
    Location
    France
    MS-Off Ver
    2010
    Posts
    260

    Re: Worksheet function with variable number of parameters

    Thanks both for the reply. The final goal of this function would be to have a formula showing in the worksheet using application.worksheetfunction so when the code runs the formula is still "dynamic".

    So something like :
    Please Login or Register  to view this content.
    Would this be possible ?
    Last edited by amphinomos; 03-15-2016 at 10:54 AM.

+ 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. Running a variable module with parameters
    By UML in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 04-04-2014, 05:22 AM
  2. [SOLVED] Replacing a referenced worksheet name with a variable worksheet index number.
    By sdavison in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-01-2013, 09:49 AM
  3. Replies: 1
    Last Post: 09-07-2012, 12:08 AM
  4. Passing multiple parameters in user defined function from worksheet
    By cmschu in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-30-2009, 02:39 AM
  5. Variable passing of parameters in a macro
    By gswhoop in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-05-2007, 03:25 AM
  6. [SOLVED] OnAction of Menu Bar with variable parameters
    By chris in forum Excel General
    Replies: 1
    Last Post: 08-07-2006, 11:10 AM
  7. Replies: 0
    Last Post: 07-17-2006, 12:10 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