+ Reply to Thread
Results 1 to 9 of 9

How to access variables that are declared in private subroutine

  1. #1
    Forum Contributor
    Join Date
    03-10-2013
    Location
    Budapest, Hungary
    MS-Off Ver
    Excel 2007
    Posts
    125

    How to access variables that are declared in private subroutine

    Dear Volunteers!

    I hope someone will help
    My question is the following:

    How to access variables that are declared in private subroutine?
    I would like to use these variables in modules.
    In order to better understanding see of the codes here below .

    Thanks for all the answers.

    Pan314


    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  2. #2
    Registered User
    Join Date
    12-10-2015
    Location
    Winnipeg
    MS-Off Ver
    2013
    Posts
    18

    Re: How to access variables that are declared in private subroutine

    You don't access variables from private subs. You need to make them global variables. I'm not sure what your trying to get answers for though, based on the code you provided. Are you trying to access a Sub-routine or a variable? They are two very different things.

  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,717

    Re: How to access variables that are declared in private subroutine

    Qualify the reference to the variable:
    Please Login or Register  to view this content.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  4. #4
    Registered User
    Join Date
    12-10-2015
    Location
    Winnipeg
    MS-Off Ver
    2013
    Posts
    18

    Re: How to access variables that are declared in private subroutine

    So you can access variables from private subs?

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

    Re: How to access variables that are declared in private subroutine

    Quote Originally Posted by TheWrapperGuy View Post
    So you can access variables from private subs?
    The variable under discussion is not a variable in a private sub; it's a Public variable in the ThisWorksheet module.

    Actually I'm surprised it's necessary to qualify a Public variable but that's what I found when I implemented this code.

    Here are the visibility rules (all the ones I can think of):

    • A variable declared as Public at the top of a generic Module is visible everywhere in every module without qualification
    • A variable declared as Public at the top of a workbook or worksheet Module is visible everywhere in that module without qualification, and everywhere in every other module with qualification
    • A variable declared in a Sub is visible only within that Sub
    • A variable declared with Dim (not Public) at the top of any module is visible only within that module
    • A variable declared in a Sub that has the same name as a variable outside the sub will hide the variable outside the sub. To reference the variable outside the sub you must add the module name as a qualifier.
    • A variable declared in a module that has the same name as a variable outside the module will hide the variable outside the sub. To reference the variable outside the module you must add the other module name as a qualifier.


    (Note that extant and scope are separate issues from visibility. The rules above address only when a variable can be legally referenced, not what happens when you reference it.)

  6. #6
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2,760

    Re: How to access variables that are declared in private subroutine

    Jeff thanks for sharing.

    At the risk of being accused of hijacking, can you elaborate on the highlighted part. I've either had too much coffee or not enough.

    A variable declared as Public at the top of a workbook or worksheet Module is visible everywhere in that module without qualification, and everywhere in every other module with qualification
    What is the qualification part?
    Click the * Add Reputation button in the lower left hand corner of this post to say thanks.

    Don't forget to mark this thread SOLVED by going to the "Thread Tools" drop down list above your first post and choosing solved.

  7. #7
    Forum Contributor
    Join Date
    03-10-2013
    Location
    Budapest, Hungary
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: How to access variables that are in private subroutine

    Dear All who responded until now!

    Probably not I formulated my problem well.
    No one can understand me.
    The reason for my imperfect English proficiency.
    I attach a file. I hope this will be understood.
    Is not explicit declaration in the file. I have no idea to this.
    Further explanation is in the file.


    Thank you for your help even more

    Sincerely Pan314

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

    Re: How to access variables that are declared in private subroutine

    Quote Originally Posted by skywriter View Post
    At the risk of being accused of hijacking, can you elaborate on the highlighted part. I've either had too much coffee or not enough.

    What is the qualification part?
    A qualification is the name of the parent object followed by a dot.

    Unqualified: Range("A8")
    Qualified: Sheet1.Range("A8")

    I don't know if VBA specifically uses that word but "qualified" is the industry standard term used for this situation in object-oriented programming, or other types of programming where containers can be named explicitly.

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

    Re: How to access variables that are in private subroutine

    Quote Originally Posted by Pan314 View Post
    I attach a file. I hope this will be understood.
    Is not explicit declaration in the file. I have no idea to this.
    Further explanation is in the file.
    This code is different than the first code you posted.

    Here is the code in ThisWorkbook:
    Please Login or Register  to view this content.
    Here is the code in Module1. Note my comment in red.
    Please Login or Register  to view this content.
    You should use Option Explicit and always require that all variables are explicitly declared. It prevents bugs like this.
    Last edited by 6StringJazzer; 12-23-2015 at 10:11 PM. Reason: Added more when I realized that the variable was not declared at all

+ 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] Declared Variables and Stored Values
    By fireflydreams in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 04-20-2015, 12:39 PM
  2. Declared variables in excel vba
    By RALIR in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-12-2015, 06:05 PM
  3. Do/Loop Until using variables declared
    By LLL0422 in forum Excel Programming / VBA / Macros
    Replies: 33
    Last Post: 06-05-2014, 09:01 AM
  4. Exit Private Subroutine
    By longbow007 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-25-2010, 01:23 AM
  5. Macro for autofilter using variables declared in worksheet
    By Dave Peterson in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 09-06-2005, 04:05 AM
  6. [SOLVED] Macro for autofilter using variables declared in worksheet
    By Jeff in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 12:05 AM
  7. Macro for autofilter using variables declared in worksheet
    By Jeff in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-05-2005, 10: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