• Passing variables in events?

    Author
    Topic
    #1768700

    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?)

    Viewing 2 reply threads
    Author
    Replies
    • #1780556

      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.

      • #1780612

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

    • #1780562

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

      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
      
      • #1780613

        >>> 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!

    • #1780777

      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

      • #1780790

        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

        • #1780846

          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

          • #1780854

            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

    Viewing 2 reply threads
    Reply To: Passing variables in events?

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: