Results 1 to 5 of 5

Thread: How to avoid clashes between variable names and built-in VBA function names

  1. #1
    New Member
    Join Date
    Jan 2011
    Posts
    19
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default How to avoid clashes between variable names and built-in VBA function names

    By chance, I discovered that Excel doesn't warn you if you declare a variable with the same name as a built-in VBA function.
    For example, the following compiles without errors:

    Code:
    Sub myProcedure
        Dim Day As String
        Day = "Monday"
    End Sub

    But now, the "Day" function cannot be used. I get an error ("Expected array") with this code:

    Code:
    Sub myProcedure
        Dim Day As String
        Day = "Monday"
        Debug.Print Day("23,5,1980")    'Offending line.
    End Sub

    A Google search has revealed a workaround: use VBA.Day to invoke the function. But I would far rather that Excel warned me not to use Day as a variable name in the first place!

    Is there a setting somewhere that prevents you from using built-in function names (such as Day, Date, Left, Right, etc.) as variable names?

    Bluto

  2. #2
    Board Regular
    Join Date
    Mar 2002
    Location
    Cincinnati, Ohio, USA
    Posts
    6,865
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Default Re: How to avoid clashes between variable names and built-in VBA function names

    There is no RULE that says that you cannot use a VBA function name in your code. Unless there is ambiguity as in your second example, it's not really a problem because within the scope of your procedure the interpreter is able to discern your intention. In any case, when you compile, you will be told if there is a problem. In that case, change the name of your variable or fully qualify the second one with VBA.X. In other words, I wouldn't worry too much about it.

  3. #3
    MrExcel MVP
    Moderator
    Peter_SSs's Avatar
    Join Date
    May 2005
    Location
    Macksville, Australia
    Posts
    40,976
    Post Thanks / Like
    Mentioned
    90 Post(s)
    Tagged
    21 Thread(s)

    Default Re: How to avoid clashes between variable names and built-in VBA function names

    A common practice is to prefix your variables with something like these that identify the declaration type
    Code:
    Dim strDay As String
    Dim blnStarted As Boolean
    Dim lngLen As Long
    This has two advantages
    1. It should avoid the problem that you have encountered.
    2. Especially if you have long code, when you read the variable name later in the code, you know what type it is declared as without having to refer back to your Dim statement
    Hope this helps, good luck.
    Peter
    Excel 365 - Windows 10
    - Want to help your helpers by posting a small, copyable, screen shot directly in your post? Look here
    - If posting vba code, please use Code Tags - like this [code]Paste code here[/code] - or use the # key in the Reply window
    - Read: Forum Rules, Forum Use Guidelines, & FAQ

  4. #4
    New Member
    Join Date
    Jan 2011
    Posts
    19
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: How to avoid clashes between variable names and built-in VBA function names

    Thank you both for your replies. I have toyed with the idea of using a naming convention of some sort, but was wondering first if there was a setting somewhere in Excel that would prevent these clashes. At least VBA sensibly prevents variable names from clashing with commands (such as Next) which would be disastrous!
    Bluto

  5. #5
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    28,078
    Post Thanks / Like
    Mentioned
    469 Post(s)
    Tagged
    47 Thread(s)

    Default Re: How to avoid clashes between variable names and built-in VBA function names

    Unfortunately there is nothing to prevent you from, or warn you about, using a keyword as a variable, or the name of a procedure.
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •