+ Reply to Thread
Results 1 to 9 of 9

How to access variables that are declared in private subroutine

Hybrid View

  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


    'stringInSubWorkBook declarated in ThisWorkbook
    'Paste into the modul1
    Public Sub TestInModul1()
    Select Case stringInSubWorkBook_Open
        Case ""
            MsgBox "stringInSubWorkBook_Open is out of context."
        Case Else
            'Here would be able to access
            MsgBox "stringInSubWorkBook_Open=" & stringInSubWorkBook_Open
    End Select
    End Sub
    'paste into the ThisWorkbook
    Public stringInSubWorkBook_Open As String
    Private Sub Workbook_Open()
        stringInSubWorkBook_Open = "testString"
        Call TestInThisWorkbook
    End Sub
    
    Public Sub TestInThisWorkbook()
        'This is OK.
        MsgBox stringInSubWorkBook_Open
    End Sub

  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
    MS 365 Family 64-bit 2505
    Posts
    27,234

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

    Qualify the reference to the variable:
    'stringInSubWorkBook declarated in ThisWorkbook
    'Paste into the modul1
    Public Sub TestInModul1()
    Select Case ThisWorkbook.stringInSubWorkBook_Open
        Case ""
            MsgBox "stringInSubWorkBook_Open is out of context."
        Case Else
            'Here would be able to access
            MsgBox "stringInSubWorkBook_Open=" & ThisWorkbook.stringInSubWorkBook_Open
    End Select
    End Sub
    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
    MS 365 Family 64-bit 2505
    Posts
    27,234

    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
    365 Version 2505
    Posts
    2,790

    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?

  7. #7
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,234

    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.

  8. #8
    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

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

    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:
    Private Sub Workbook_Open()
        stringInSubWorkBook_Open = "testString" 'stringInSubWorkBook_Open-called variable, which must be accessed in the modul1.
        Call TestInModul1 'Here it is only for the purpose of testing
    End Sub
    Here is the code in Module1. Note my comment in red.
    Public Sub TestInModul1()
    Select Case stringInSubWorkBook_Open
        Case ""
            MsgBox "stringInSubWorkBook_Open is out of context."
        Case Else
            'Here I should definitely have access to the stringInSubWorkBook_Open variable
            Absolutely not. stringInSubWorkBook_Open is not explicitly declared,
            so VBA considers it to be implicitly declared inside Sub Workbook_Open because that is where it is used.
            If you review my visibility rules you will see that this variable cannot be visible outside the Sub.
            In your first example, it was declared outside that Sub and could be made visible with 
            qualification as shown in red in my code example.
    
            Further, it is not declared in TestInModul1 either, so it is also declared implicitly there.
    
            You have two different variables in two different Subs that happen to have the same name.
            
            'It is now only available in the ThisWorkbook
            MsgBox stringInSubWorkBook_Open '
    End Select
    End Sub
    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. 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