'How to call a userform's subroutine within a a separate module's subroutine with VBA

I am wondering how I can call a public subroutine that resides within a userform of two option buttons. Specifically if that user selections optionbutton1 from that userform is selected, then run some code from a sub routine within a module.

In the UserForm code:

    Public Sub OptionButton1_Click()
        Optionbutton1 = True
        Optionbutton2 = False
    End Sub
    
    Public Sub OptionButton2_Click()
        Optionbutton1 = False
        Optionbutton2 = True
    End Sub
    
    Public Sub TextBox1_Change()
    
    End Sub

In Module:

   Global Optionbutton1 As Integer
   Global Optionbutton2 As Integer 
-------------------------------------------------------------  
   Sub ProjectSetup(Optionbutton1. Optionbutton2)
       
       Call UserForm1.OptionButton1_Click
       Call UserForm1.OptionButton2_Click
       
       If OptionButton1 = True then
           [do some action]
   
       If OptionButton2 = True then
           [do some action]

I think my issue is based on how I am trying to call in the subroutines "OptionButton1_Click" and "OptionButton2_Click" from UserForm1. When I run the code above I get a compiling error that states the function or sub not defined.

Thanks for any help!



Solution 1:[1]

This is not the right way to do whatever it is you're doing, but this would work.

In UserForm1:

Public Sub OptionButton1_Click()
    gOpt1 = True  'setting the public globals declared in the calling module
    gOpt2 = False
End Sub

Public Sub OptionButton2_Click()
    gOpt1 = False
    gOpt2 = True
End Sub

In a regular module:

Option Explicit

Public gOpt1 As Boolean
Public gOpt2 As Boolean

Sub Tester()

    Dim frm As UserForm1
    
    Set frm = New UserForm1
    
    gOpt1 = False 'reset globals
    gOpt2 = False
    
    frm.Show vbModeless 'Must be modeless, or the code stops here
                        '  until the form is closed
    
    Debug.Print gOpt1, gOpt2  '> False, False
    frm.OptionButton1_Click
    Debug.Print gOpt1, gOpt2  '> True, False
    frm.OptionButton2_Click
    Debug.Print gOpt1, gOpt2  '> False, True
    
    Unload frm

End Sub

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim Williams