Results 1 to 9 of 9
  1. #1
    sgerber
    Guest

    Passing variables in events?

    I get the feeling I'm opening a major can of worms for myself with this question, but...

    Is it possible to pass the value of a variable from one event to another within the code for a userform?

    (Does that question even make any sense?)

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Passing variables in events?

    Something like -

    Call CommandButton1_Click(myParameter)

    ? I haven't tried it, but perhaps you could create an optional parameter for an event procedure. Probably easiest to use module-level variables.

  3. #3
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Passing variables in events?

    I'd pass it via variables scoped for the userform- ie, defined before any procedures:

    <pre>Option Explicit
    Private strMyString As String

    Private Sub cmdCancel_Click()
    strMyString = "Cancel Pressed"
    End Sub

    Private Sub cmdOK_Click()
    MsgBox strMyString
    End Sub

    Private Sub UserForm_Initialize()
    strMysString = "Start Value"
    End Sub
    </pre>

    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  4. #4
    sgerber
    Guest

    Re: Passing variables in events?

    >>> Something like -

    Call CommandButton1_Click(myParameter) <<<

    This one didn't work, unfortunately. The error message read: "Procedure declaration does not match description of event or procedure having the same name."

    Thanks for taking a shot, though.

  5. #5
    sgerber
    Guest

    Re: Passing variables in events?

    >>> Option Explicit
    Private strMyString As StringPrivate
    Sub cmdCancel_Click()
    strMyString = "Cancel Pressed"
    End Sub ... <<<

    This led me to a solution -- and a learning experience.

    I was unloading the userform in the first line of my CmdOK_Click() procedure. I couldn't figure out why your example worked and my variation on it didn't, until I realized that unloading the form also wipes out the values of any variables declared in the initialization procedure.

    Thanks very much for the assist!

  6. #6
    Lounger
    Join Date
    Nov 2002
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Passing variables in events?

    I've a similar problem. I'd like to do something like:

    Sub PutNumber(Number)
    Range("B3").Select
    ActiveCell.Value = Number
    End Sub

    Then have a button on my workbook that would be attached to a macro called PutNumber 5 for example. But this doesn't work. Can you actually define a variable in this way? if not then what are the ()s for?

    Bob

  7. #7
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Passing variables in events?

    Of course you can define a variable that way. It should work. You might want to delare the type instead of taking the default. But it should not matter.

    Sub PutNumber(Number as Long) 'If you don't declare Excel defaults to Variant
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  8. #8
    Lounger
    Join Date
    Nov 2002
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Passing variables in events?

    I just can't get it to work for me and I've tried and tried. I am a beginner so I've never actually done it at all. Someone wouldn't like (mind) puttin together an example for me would they?
    eg
    Sub message(text)
    msgbox(text)
    end sub

    then place a command button on a page assigned to macro:
    Message ("hello world").

    Bob

  9. #9
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Passing variables in events?

    I really don't get where your parameter value comes from. Please clarify. By page do you mean a Word document or Userform? Since you cannot pass a parameter into a clicked event, you have to compromise. A command button named Button on a Word document can be pressed to display "Hello World" But somehow I don't think this is what you are after.

    In the Thisdocument object:

    Dim strMsg As String

    Private Sub Button_Click()
    Call message("Hello World")
    End Sub

    Sub message(strIn As String)
    MsgBox strIn
    End Sub
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •