+ Reply to Thread
Results 1 to 10 of 10

Declare global variable in Application level

Hybrid View

  1. #1
    Registered User
    Join Date
    11-10-2008
    Location
    India
    Posts
    35

    Thumbs up Declare global variable in Application level

    Hi,
    I need to declare a global variable in Application level not Module level.
    I have declare a variable in Module1 and then Module2 with same name then complile it and get success. That means there has two variable with same name in different Module. I think this was not a proper global variable declaration by which I can allow to declare only one variable in all Module, Class every where. I have used code as below -

    Option Explicit
    Public MyID As Integer
    Thanks,
    Suman
    Last edited by suman.biswas; 09-18-2009 at 04:31 AM.

  2. #2
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264

    Re: Declare global variable in Application level

    It will only register it as an ambiguous name if you try and use the MyID variable in a module which isn't either of the Standard modules in which you have declared the variable.

    If you use it in a sub or function in either of the standard modules in which it has been decalred, if you do not prefix the variable with the module in which it has been declared eg:

    MsgBox Module1.MyID
    VBA will assume you meant to reference the MyID that was declared in the module in which the code resides (hence it doesn not think there is an ambiguity as you have only declared the variable publicly once in each module).
    Richard Schollar
    Microsoft MVP - Excel

  3. #3
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Declare global variable in Application level

    Suman.biwas
    Could you store the values in a text file in the temp folder for future reference?
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Declare global variable in Application level

    You can declare a global variable in a standard module dedicated to globals. You can then use that global variable in any module that you want.
    Attached Files Attached Files
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  5. #5
    Registered User
    Join Date
    11-10-2008
    Location
    India
    Posts
    35

    Re: Declare global variable in Application level

    Hi,
    Thanks to all for your valuable reply.
    I have seen Richard Schollar's example is use full to me. But I have tried with RoyUK's example but and tried to declare more variable in each module and get success. That means these are all separate variable and total three different variables. So these can't work as global variable.

    Suman
    Attached Files Attached Files

  6. #6
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Declare global variable in Application level

    True

    My first post was misguided I was think of another post, which I can find,
    where you could save names or values ect in a txt file hidden in the temp folder for reference when work book was re opened in a non savable work book

    looks like richard and roy have answer the question

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Declare global variable in Application level

    I don't see why you have tried to declare the variable in each module, it is already declared in Module1. Did you run the test macros before changing my code?

    Explain clearly what you are attempting.

  8. #8
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Declare global variable in Application level

    Hi royUK
    I think it was an example of what didnt/cant work

  9. #9
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Declare global variable in Application level

    Another attempt at wording what Richard and Roy have already said


    A variable declared as public at a module level is available to ALL modules.

    If you declare the variable in each module with the same name then it will be treated as a unique variable within each of the modules.
    BUT if you try to reference the variable name from a module where you did not declare it the complier can not determine which of the many declarations you mean and so raises a compiler error.

    Simple declare the variable once.
    Cheers
    Andy
    www.andypope.info

  10. #10
    Registered User
    Join Date
    11-10-2008
    Location
    India
    Posts
    35

    Re: Declare global variable in Application level

    Hi,
    I have tried with RoyUK's example and it works fine as global variable. But if I declare duplicate variable bymistake in my project then it will give problem in run time. So I was thing to make some way to declare unique global variable.

    Thanks all already I have got my solution.

    Suman

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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