+ Reply to Thread
Results 1 to 7 of 7

Private modules and global variables

  1. #1
    Forum Contributor
    Join Date
    03-03-2005
    Posts
    316

    Private modules and global variables

    While subroutines in private modules (as in userforms) can access global variables in general modules publicly declared , I am having trouble engineeering a reverse flow ( to transfer variable values across from private to general module).

    Is there a way codes in a general module can access variables generated in Userform or Class Modules, assuming these are declared public at the module level?


    Thanks for any help.


    David

  2. #2
    Bob Phillips
    Guest

    Re: Private modules and global variables

    Yes, declare them as Public as usual, and when accessing them qualify with
    the class name, such as

    Userform1.myVar

    or

    Sheet1.myVar

    --

    HTH

    RP
    (remove nothere from the email address if mailing direct)


    "davidm" <[email protected]> wrote in
    message news:[email protected]...
    >
    > While subroutines in private modules (as in userforms) can access global
    > variables in general modules *publicly declared *, I am having trouble
    > engineeering a reverse flow ( to transfer variable values across from
    > private to general module).
    >
    > Is there a way codes in a general module can access variables
    > generated in Userform or Class Modules, assuming these are declared
    > public at the module level?
    >
    >
    > Thanks for any help.
    >
    >
    > David
    >
    >
    > --
    > davidm
    > ------------------------------------------------------------------------
    > davidm's Profile:

    http://www.excelforum.com/member.php...o&userid=20645
    > View this thread: http://www.excelforum.com/showthread...hreadid=488169
    >




  3. #3
    Forum Contributor
    Join Date
    03-03-2005
    Posts
    316
    Hi Bob,

    Thanks for your reply. I am however still at sea. Suppose, for an illustration, I have a code in a Userform module that counts the number of controls on the Form and wish to retieve the count in a general module. The standard code in the USerform module will look like:

    Private Sub ShowNumberofControls()
    Dim c as Control
    For Each c In Me.Controls
    k = k + 1
    Next

    ...And in the general module

    Sub RetrieveCounter()
    Call ShowNumberOfControls
    Msgbox Userform1.k
    End Sub


    The above stalls at Call ShowNumberOfControls with a Complie Error: Sub or Function not defined message.


    Have I misconstrued your guidance?

    David.

  4. #4
    Dave Peterson
    Guest

    Re: Private modules and global variables

    I put this behind the userform:

    Option Explicit
    Public k As Long
    Sub ShowNumberofControls()
    Dim c As Control
    k = 0
    For Each c In Me.Controls
    k = k + 1
    Next c
    'k = me.Controls.Count 'maybe better???
    End Sub

    And I put this in a general module:
    Option Explicit
    Sub RetrieveCounter()
    Call UserForm1.ShowNumberofControls
    MsgBox UserForm1.k
    End Sub

    ===

    Or I could dump the routine behind the userform and use:

    Sub retrieveCounter2()
    MsgBox UserForm1.Controls.Count
    End Sub

    davidm wrote:
    >
    > Hi Bob,
    >
    > Thanks for your reply. I am however still at sea. Suppose, for an
    > illustration, I have a code in a Userform module that counts the number
    > of controls on the Form and wish to retieve the count in a general
    > module. The standard code in the USerform module will look like:
    >
    > Private Sub ShowNumberofControls()
    > Dim c as Control
    > For Each c In Me.Controls
    > k = k + 1
    > Next
    >
    > ..And in the general module
    >
    > Sub RetrieveCounter()
    > Call ShowNumberOfControls
    > Msgbox Userform1.k
    > End Sub
    >
    > The above stalls at *Call ShowNumberOfControls *with a *Complie Error:
    > Sub or Function not defined* message.
    >
    > Have I misconstrued your guidance?
    >
    > David.
    >
    > --
    > davidm
    > ------------------------------------------------------------------------
    > davidm's Profile: http://www.excelforum.com/member.php...o&userid=20645
    > View this thread: http://www.excelforum.com/showthread...hreadid=488169


    --

    Dave Peterson

  5. #5
    Bob Phillips
    Guest

    Re: Private modules and global variables

    I never said that you can run a procedure in the userform, you can, but that
    is not what I said. I just showed you how to get the userform variable.

    To do that, you need to declare the procedure as public and use this code

    Sub RetrieveCounter()
    Load UserForm1
    UserForm1.ShowNumberofControls
    MsgBox UserForm1.k
    Unload Userform1
    End Sub

    Don't forget to declare k as a modula public variable else it will be local
    to the procedure.

    --

    HTH

    RP
    (remove nothere from the email address if mailing direct)


    "davidm" <[email protected]> wrote in
    message news:[email protected]...
    >
    > Hi Bob,
    >
    > Thanks for your reply. I am however still at sea. Suppose, for an
    > illustration, I have a code in a Userform module that counts the number
    > of controls on the Form and wish to retieve the count in a general
    > module. The standard code in the USerform module will look like:
    >
    > Private Sub ShowNumberofControls()
    > Dim c as Control
    > For Each c In Me.Controls
    > k = k + 1
    > Next
    >
    > ..And in the general module
    >
    > Sub RetrieveCounter()
    > Call ShowNumberOfControls
    > Msgbox Userform1.k
    > End Sub
    >
    >
    > The above stalls at *Call ShowNumberOfControls *with a *Complie Error:
    > Sub or Function not defined* message.
    >
    >
    > Have I misconstrued your guidance?
    >
    > David.
    >
    >
    > --
    > davidm
    > ------------------------------------------------------------------------
    > davidm's Profile:

    http://www.excelforum.com/member.php...o&userid=20645
    > View this thread: http://www.excelforum.com/showthread...hreadid=488169
    >




  6. #6
    Forum Contributor
    Join Date
    03-03-2005
    Posts
    316
    Hello Bob,

    Excuse my obtuseness. If the procedure

    Sub RetrieveCounter()
    Load UserForm1
    UserForm1.ShowNumberofControls
    MsgBox UserForm1.k
    Unload Userform1
    End Sub

    is run from a general module, it throws up another compile error message: Member or data member not found with both the sub title Sub RetrieveCounter()and UserForm1.ShowNumberofControls highlighted. In all this, k is declared Public at the Userform code module level and should be truly global. I am using Xl2000 and could this put down to paying the laggards' price?

    David.

  7. #7
    Bob Phillips
    Guest

    Re: Private modules and global variables

    Not sure David.

    I tried her also on XL 2000, and it worked fine.

    Want to post a workbook to me?

    --

    HTH

    RP
    (remove nothere from the email address if mailing direct)


    "davidm" <[email protected]> wrote in
    message news:[email protected]...
    >
    > Hello Bob,
    >
    > Excuse my obtuseness. If the procedure
    >
    > Sub RetrieveCounter()
    > Load UserForm1
    > UserForm1.ShowNumberofControls
    > MsgBox UserForm1.k
    > Unload Userform1
    > End Sub
    >
    > is run from a general module, it throws up another compile error
    > message: *Member or data member not found* with both the sub title Sub
    > RetrieveCounter()and UserForm1.ShowNumberofControls highlighted. In all
    > this, k is declared *Public* at the Userform code module level and
    > should be truly global. I am using Xl2000 and could this put down to
    > paying the laggards' price?
    >
    > David.
    >
    >
    > --
    > davidm
    > ------------------------------------------------------------------------
    > davidm's Profile:

    http://www.excelforum.com/member.php...o&userid=20645
    > View this thread: http://www.excelforum.com/showthread...hreadid=488169
    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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