+ Reply to Thread
Results 1 to 3 of 3

Reference to a different Sub

  1. #1
    Registered User
    Join Date
    03-27-2006
    Posts
    1

    Reference to a different Sub

    I’d appreciate you answering this question for me.


    I have prompted the user for an input in one of my subs:
    Private Sub CommandButton1_Click()

    And now I need to use this data in a different sub:
    Private Sub CommandButton5_Click()

    How do I reference to a different sub to get my value?

    Thank you.

  2. #2
    Rick Hansen
    Guest

    Re: Reference to a different Sub

    pontiy, You can't reference another sub, but you can keep the data from
    that sub by declaring a Public variable. See example below. This way the
    data is availble for any sub you wish to use it in.

    enjoy, Rick

    PS: Declare the public variable at top of UserForm Code, or in Code
    Module...


    Option Explicit
    Public Ans as Variant

    Private Sub CommandButton1_Click()

    Ans = InPutBox(prompt:="What is your answer?")
    .....
    End Sub

    Private Sub CommandButton5_Click()
    Range("A1").value = Ans

    End sub


    "pontiy" <[email protected]> wrote in
    message news:[email protected]...
    >
    > I'd appreciate you answering this question for me.
    >
    >
    > I have prompted the user for an input in one of my subs:
    > Private Sub CommandButton1_Click()
    >
    > And now I need to use this data in a different sub:
    > Private Sub CommandButton5_Click()
    >
    > How do I reference to a different sub to get my value?
    >
    > Thank you.
    >
    >
    > --
    > pontiy
    > ------------------------------------------------------------------------
    > pontiy's Profile:

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




  3. #3
    Jim Thomlinson
    Guest

    RE: Reference to a different Sub

    So in button 1 you get some data from the user. Where do you store that data?
    Bare in mind that the variables you declare in a sub last only as long as
    that sub is running. Whe th ebub ends the data in the varaible is lost. If
    you want to use that data later you need to store it somewhere other than the
    sub, such as in a global variable declared at the top of the code module, or
    directly in a cell in the spreadsheet.
    --
    HTH...

    Jim Thomlinson


    "pontiy" wrote:

    >
    > I’d appreciate you answering this question for me.
    >
    >
    > I have prompted the user for an input in one of my subs:
    > Private Sub CommandButton1_Click()
    >
    > And now I need to use this data in a different sub:
    > Private Sub CommandButton5_Click()
    >
    > How do I reference to a different sub to get my value?
    >
    > Thank you.
    >
    >
    > --
    > pontiy
    > ------------------------------------------------------------------------
    > pontiy's Profile: http://www.excelforum.com/member.php...o&userid=32885
    > View this thread: http://www.excelforum.com/showthread...hreadid=528325
    >
    >


+ 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