+ Reply to Thread
Results 1 to 5 of 5

How to pass named arguments to custom (UDF) functions in Excel 2016

  1. #1
    Registered User
    Join Date
    08-08-2017
    Location
    Coral Springs, Florida
    MS-Off Ver
    2016
    Posts
    2

    How to pass named arguments to custom (UDF) functions in Excel 2016

    This post is not about passing optional arguments within VBA. This is about passing optional arguments from the spreadsheet to a custom function written in VBA. I've been surprised to find not a single mention of passing optional arguments, especially named parameters, from the spreadsheet.

    My current work-around is to use the "f"unction pop-up to build a list with empty commas, for example =YesNo(B4,,,,,"value"), but I'd much rather pass named parameters if possible. I also discovered Application.MacroOptions, which makes the work-around work better.

    Here's my header. The function works (body not included) but would be even easier to use if I can supply named parameters from the Excel spreadsheet formula:

    Public Function YesOrNo(InputValue, _
    Optional InvalidResponse As String = "xlErrValue", _
    Optional AcceptTF As Boolean = True, _
    Optional Accept01 As Boolean = True, _
    Optional AcceptTrueFalse As Boolean = True, _
    Optional YesValue = "Yes", _
    Optional NoValue = "No") As String

    YesNo = "Of Course" ' dummy function body

    Exit Function

    Is there a way to pass optional arguments by name, from the spreadsheet to the VBA custom function (aka UDF) ???


    Thanks!

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: How to pass named arguments to custom (UDF) functions in Excel 2016

    You would pass the named variables as Strings

    You can use a named range thus:-

    Please Login or Register  to view this content.

    You can read a named variable using Evaluate. In this example MyRange and Test are excel variables.

    Please Login or Register  to view this content.
    Last edited by mehmetcik; 08-08-2017 at 04:05 PM.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Registered User
    Join Date
    08-08-2017
    Location
    Coral Springs, Florida
    MS-Off Ver
    2016
    Posts
    2

    Re: How to pass named arguments to custom (UDF) functions in Excel 2016

    mehmetcik, thank you for responding, but your response is not related to my question as it includes no mention of passing parameters to custom functions. named ranges and named parameters are not the same thing.

    In SQL Server, for example, a procedure call using named parameters might appear as follows:

    EXECUTE YesOrNo "true", AcceptTF = false, NoValue = "negative"

    or in c#:

    result = YesOrNo(MyVariable, AcceptTF:false, NoValue:"negative");

    If Excel supports named parameters, then a spreadsheet formula call a compatible function might appear as follows (in a cell on the spreadsheet):

    =YesOrNo(B4, AcceptTf = false, NoValue = "negative")

    instead of:

    =YesOrNo(B4, , false, , , , "negative")

    When I wrote that I was surprised to see not even a single mention of named parameters, that means that I did not find out how to do it, or even find reports of other people asking about it. Named parameters may not be a feature yet in Excel formulas, but they are common elsewhere (I omitted any mention of VBA / VB to avoid confusion with calls from one VBA function to another).

  4. #4
    Registered User
    Join Date
    12-18-2019
    Location
    Raleigh, North Carolina, USA
    MS-Off Ver
    2013
    Posts
    1

    Re: How to pass named arguments to custom (UDF) functions in Excel 2016

    I too am looking for a way to pass named arguments from the spreadsheet using a UDF call to a vba function having multiple optional named arguments. Basically I want to calculate a property that can be derived from a variety of different inputs. In other programming languages I would create an overloaded function. In VBA I can accomplish the same basic functionality using a generic UDF call to a vba function having a large number of optional arguments. At the same time I write several variants of the function each having a different name that are setup to solve for the desired property from each of the appropriate combinations of input arguments. The generic function call contains logic to determine which of the optional arguments are present, validates that it is an appropriate combination, and then passes those arguments to the appropriate variant of the function and then returns the solution back to the cell. This all works, BUT, the long list of optional arguments on the generic call is awkward. Similarly requiring the user to remember the name of which variant of the function to call is also not desired. If I could find an elegant method of passing named arguments from a UDF I believe I would have a satisfactory solution

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,016

    Re: How to pass named arguments to custom (UDF) functions in Excel 2016

    You cannot pass named arguments in a worksheet function; they can only be passed by position.
    Everyone who confuses correlation and causation ends up dead.

+ 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. Why won't Excel 2016 display my custom ribbon?
    By rkjudy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-05-2015, 04:20 PM
  2. [SOLVED] Do User-Defined-Functions accept named arrays as arguments?
    By RogeratCCCC in forum Excel - New Users/Basics
    Replies: 9
    Last Post: 05-02-2012, 04:17 AM
  3. Named Ranges In Custom Functions
    By djt76010 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 12-03-2008, 02:20 PM
  4. Passing Constant Arguments to custom Subroutine & Functions
    By cLiffordiL in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-21-2006, 04:20 AM
  5. Custom functions calculating time arguments Help Desperate
    By Bill_De in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 04-24-2006, 09:25 PM
  6. Replies: 0
    Last Post: 02-03-2006, 06:20 AM
  7. Custom functions using arguments with same name
    By Spencer Hutton in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-27-2005, 02:05 PM

Tags for this Thread

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