Pass boolean to userform

kpasa

Board Regular
Joined
Nov 20, 2015
Messages
82
I am trying to pass a boolean from a sub into a userform. I want to activate a userform with a boolean named PrintVal set to true by this particular macro.

On userform_activate, If printval is true then i want to run a special bit of code. Else just launch the form like normal.

How can I pass the boolean to a userform from the sub that activated it.
 

Some videos you may like

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Trebor76

Well-known Member
Joined
Jul 23, 2007
Messages
4,587
Hi kpasa,

Just set the value of the property before you activate the form i.e. this will set the value of a text box called txtPrintVal prior to opening an user form called UserForm1:

Code:
Option Explicit
Sub Macro1()

    With UserForm1
        .txtPrintVal = 1 '1 for True or 0 for False
        .Show
    End With

End Sub
You can then check what has been assigned to the txtPrintVal text box when the form is activated via userform_activate as you say.

HTH

Robert
 

kpasa

Board Regular
Joined
Nov 20, 2015
Messages
82
In this instance, PrintVal is a boolean. It's not a value assigned to an object. How can i set a boolean using your method?
 
Last edited:

kpasa

Board Regular
Joined
Nov 20, 2015
Messages
82
Yes, and I read them. I might try the Tag, but the Property example is a different setup than I have. I have a module sub that opens a userform, not a sub within a userform (like the example shows).
There are two other methods outlined in that article...
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,139
You can still use the Property statement. In your case, though, since you want to pass a boolean value, your code in the userform would be something like this...

Code:
[FONT=Verdana][COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit[/COLOR]

[COLOR=darkblue]Private[/COLOR] pPrintVal [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Boolean[/COLOR]

[COLOR=darkblue]Property[/COLOR] [COLOR=darkblue]Let[/COLOR] PrintVal(v [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Boolean[/COLOR])
    pPrintVal = v
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Property[/COLOR]

[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] UserForm_Activate()
    MsgBox pPrintVal
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR][/FONT]
So this creates a property called PrintVal for the userform. Then, when the userform is activated, the boolean value is made available. For your procedure that shows the userform, you would have the following...

Code:
[FONT=Verdana][COLOR=darkblue]Sub[/COLOR] test()

    UserForm1.PrintVal = [COLOR=darkblue]True[/COLOR]
    
    UserForm1.Show
      
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR][/FONT]
So, as you can see, first the PrintVal property is set to True, and then the userform is shown. And, so when the userform is activated, this boolean value is made available.

Does this help?
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,325
A different approach might use the Tag property of the user form

Code:
UserForm1.Tag = printVal
UserForm1.Show
Code:
Private Sub UserForm_Activate()
    If Me.Tag = "True" Then
        Rem call routine
    End If
End Sub

A different approach would be to write a different way to reveal the user form

Code:
' in the UF code module

Dim PrintVal as Boolean

Public Sub Display(blnPrintVal as Boolean)
    PrintVal = blnPrintVal
    Me.Show
End Sub

Private Sub Userform_Activate()
    If PrintVal Then 
        ' do somehting
    End If
End Sub
In the calling code

Code:
Userform1.Display(True)
' or 
UserForm1.Display(False)
 

Watch MrExcel Video

Forum statistics

Threads
1,096,185
Messages
5,448,852
Members
405,533
Latest member
Heretical1

This Week's Hot Topics

Top