Thanks Thanks:  0
Likes Likes:  0
Results 1 to 9 of 9

Thread: Pass boolean to userform

  1. #1
    Board Regular kpasa's Avatar
    Join Date
    Nov 2015
    Location
    DFW, TX
    Posts
    71
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Pass boolean to userform

    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.

  2. #2
    MrExcel MVP
    Join Date
    Mar 2004
    Location
    Canada
    Posts
    18,305
    Post Thanks / Like
    Mentioned
    11 Post(s)
    Tagged
    3 Thread(s)

    Default Re: Pass boolean to userform

    Have a look at the following link...

    Passing Arguments to a Userform – Daily Dose of Excel

  3. #3
    Board Regular kpasa's Avatar
    Join Date
    Nov 2015
    Location
    DFW, TX
    Posts
    71
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Pass boolean to userform

    I should have started with: I don't want to use public variables. I tried it and it causes other issues. The contributor in the comments of that link said it perfectly. I want my code encapsulated.
    Quote Originally Posted by Domenic View Post
    Have a look at the following link...

    Passing Arguments to a Userform – Daily Dose of Excel

  4. #4
    MrExcel MVP
    Join Date
    Mar 2004
    Location
    Canada
    Posts
    18,305
    Post Thanks / Like
    Mentioned
    11 Post(s)
    Tagged
    3 Thread(s)

    Default Re: Pass boolean to userform

    There are two other methods outlined in that article...

  5. #5
    Board Regular
    Join Date
    Jul 2007
    Location
    Sydney
    Posts
    4,189
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    1 Thread(s)

    Default Re: Pass boolean to userform

    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

  6. #6
    Board Regular kpasa's Avatar
    Join Date
    Nov 2015
    Location
    DFW, TX
    Posts
    71
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Pass boolean to userform

    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 by kpasa; Jul 6th, 2017 at 09:22 AM.

  7. #7
    Board Regular kpasa's Avatar
    Join Date
    Nov 2015
    Location
    DFW, TX
    Posts
    71
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Pass boolean to userform

    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).
    Quote Originally Posted by Domenic View Post
    There are two other methods outlined in that article...

  8. #8
    MrExcel MVP
    Join Date
    Mar 2004
    Location
    Canada
    Posts
    18,305
    Post Thanks / Like
    Mentioned
    11 Post(s)
    Tagged
    3 Thread(s)

    Default Re: Pass boolean to userform

    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:
    Option Explicit
    
    Private pPrintVal As Boolean
    
    Property Let PrintVal(v As Boolean)
        pPrintVal = v
    End Property
    
    Private Sub UserForm_Activate()
        MsgBox pPrintVal
    End Sub
    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:
    Sub test()
    
        UserForm1.PrintVal = True
        
        UserForm1.Show
          
    End Sub
    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?

  9. #9
    MrExcel MVP mikerickson's Avatar
    Join Date
    Jan 2007
    Location
    Davis CA
    Posts
    22,094
    Post Thanks / Like
    Mentioned
    13 Post(s)
    Tagged
    8 Thread(s)

    Default Re: Pass boolean to userform

    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)

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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