Results 1 to 7 of 7

VBA: Setting a Global Variable to a Value

This is a discussion on VBA: Setting a Global Variable to a Value within the Excel Questions forums, part of the Question Forums category; I have a global variable called "Start" which I set equal to the Timer so I can subtract that from ...

  1. #1
    Board Regular
    Join Date
    May 2004
    Posts
    285

    Default VBA: Setting a Global Variable to a Value

    I have a global variable called "Start" which I set equal to the Timer so I can subtract that from the current time to record the elapsed time that a macro is running. I set this by using:
    Code:
    Option Explicit
    Public Start As Single
    At the beginning of a sub, I define this global variable as Start = Timer. The sub goes to call another sub where the Start variable is also used. When it gets to the code within this second sub, it gives me an error called "ambiguous name detected: Start". What am I doing wrong? Am I not defining the global variable correctly? Is there a way to define the variable as a specific value globally? Do I have to put an identical Option Explicit code at the top of each module in which all subs are stored?

    I appreciate the insight into this whole global variable problem.

    EDIT: All subs called are actually defined as "Functions", if that matters.

  2. #2
    Board Regular Norie's Avatar
    Join Date
    Apr 2004
    Location
    Stirling
    Posts
    66,267

    Default

    Have you declared Start more than once?
    If posting code please use code tags.

  3. #3
    Board Regular
    Join Date
    May 2004
    Posts
    285

    Default

    No, Start is only declared in the initial Option Explicit section of code.

  4. #4
    Board Regular Norie's Avatar
    Join Date
    Apr 2004
    Location
    Stirling
    Posts
    66,267

    Default

    And it isn't declared in anywhere else? In another module for example.
    If posting code please use code tags.

  5. #5
    Board Regular
    Join Date
    May 2004
    Posts
    285

    Default

    Yes, it's in another module, but would Excel be looking at all modules for variables even if they are not being executed at the time? I was thinking that the public declaration only applies to the code being executed in the module at the time.

  6. #6
    Board Regular Norie's Avatar
    Join Date
    Apr 2004
    Location
    Stirling
    Posts
    66,267

    Default

    By declaring iy as Public it is available to all modules.

    So if you have it declared in more than 1 module you have an ambiguous name.
    If posting code please use code tags.

  7. #7
    Board Regular
    Join Date
    May 2004
    Posts
    285

    Default

    I didn't believe that Excel worked like that. Now I understand. A variable name adjustment should fix it. Thanks for all your help and info, Norie!

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
  •  


DMCA.com