+ Reply to Thread
Results 1 to 6 of 6

Using a variable as an optional parameters default

  1. #1
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Using a variable as an optional parameters default

    Hi all.

    You see when you make a procedure that has an optional parameter and you want to give the optional parameter a default, you do something like:
    Please Login or Register  to view this content.
    or you can use a constant (either a system constant or a user defined constant will work) for the default like:
    Please Login or Register  to view this content.
    So that's all cool.
    But I tried to use a variable for the default and Excel wasn't having any of it. I get an error message saying "Compile error: Constant expression required". So that means I can't use a variable! FS

    In my code there is a variable that holds the name of the current "thing" that the code is working on (a filename actually). When I call this particular procedure, I want it to execute based on whatever I pass to it as the parameter, or if there's no parameter passed, I want it to execute on whatever the current "thing" is that the code is working on instead.

    It seems to me that I should be able to use a variable but clearly I'm not allowed.

    Anyone any idea how I can achieve something similar here?
    Please click *Add Reputation if I've helped

  2. #2
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Using a variable as an optional parameters default

    OK, so I've just realised that within the procedure I can test if the parameter holds the default value, say 'vbNullstring" (or more likely, something crazier that definitely won't be passed like "azazazazaz"), and then if it does, just replace the value of the parameter with the value of my variable.
    Please Login or Register  to view this content.
    So, I answered my own thread. But anyone have any idea why you can't just include a variable there? It seems reasonable.

  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Using a variable as an optional parameters default

    The value for an optional parameter default must be known at compile time, so it cannot contain a variable or any other expression that is known only at runtime (like a function call). The code for a Sub call is generated at compile time, not evaluated at runtime. This is a fundamental VBA design decision, and IMHO a good one. When you start doing things like dynamic defaults it creates all kinds of opportunities to introduce bugs that are very difficult to diagnose.

    Where is the filename variable declared relative to your Sub? You have two options:

    1. Simply pass the file name as the parameter.

    2. If the filename variable is visible within the scope of the Sub, you can do this:

    Please Login or Register  to view this content.
    Frankly, I am not so sure your desired approach is a sound software design, but it's hard to say without seeing all your code.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  4. #4
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Using a variable as an optional parameters default

    Quote Originally Posted by kadeo View Post
    Hi all.

    You see when you make a procedure that has an optional parameter and you want to give the optional parameter a default, you do something like:
    Please Login or Register  to view this content.
    or you can use a constant (either a system constant or a user defined constant will work) for the default like:
    Please Login or Register  to view this content.
    So that's all cool.
    But I tried to use a variable for the default and Excel wasn't having any of it. I get an error message saying "Compile error: Constant expression required". So that means I can't use a variable! FS

    In my code there is a variable that holds the name of the current "thing" that the code is working on (a filename actually). When I call this particular procedure, I want it to execute based on whatever I pass to it as the parameter, or if there's no parameter passed, I want it to execute on whatever the current "thing" is that the code is working on instead.

    It seems to me that I should be able to use a variable but clearly I'm not allowed.

    Anyone any idea how I can achieve something similar here?
    It would be easier to analyze if you posted the problem code. Basically, you should be able to declare the variable in the same manner that you have shown above. But subsequent use of the variable in the code could be causing the compiler to hiccup.

  5. #5
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Using a variable as an optional parameters default

    Quote Originally Posted by 6StringJazzer View Post
    The value for an optional parameter default must be known at compile time...
    Well, that explains it then. I don't really get yet which things have to be known at compile time and which don't but if that's the case for optional parameters then at least it makes sense to me now why I can't use a variable. Thanks for the knowledge boost Jazzer.

  6. #6
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Using a variable as an optional parameters default

    Please Login or Register  to view this content.

+ 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. [SOLVED] Is there an Optional Parameters to Clear on .Insert
    By dlow in forum Excel General
    Replies: 3
    Last Post: 03-18-2015, 11:54 PM
  2. Do you set default values for optional paramaters?
    By dmasters4919 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-11-2015, 02:15 PM
  3. [SOLVED] Reading optional parameters in a function
    By TTech925 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-17-2014, 05:53 PM
  4. Optional Variable
    By Jacques Grobler in forum Excel General
    Replies: 6
    Last Post: 09-13-2011, 01:58 AM
  5. Argument not optional error while passing value of variable between subs
    By asha3010 in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 09-30-2010, 05:21 AM
  6. Representation of optional parameters in Function arguments window
    By compound in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-17-2006, 07:32 PM
  7. Replies: 9
    Last Post: 09-07-2005, 03: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