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.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
There are two other methods outlined in that article...
 
Upvote 0
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
 
Upvote 0
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:
Upvote 0
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...
 
Upvote 0
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?
 
Upvote 0
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)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top