UserForm w/2 modules?

cstimart

Well-known Member
Joined
Feb 25, 2010
Messages
1,180
Is it possible to have macro from separate modules utilize the same UserForm? I've tried to do such, but no luck, as I have had to reference specific to the sub/macro that utilizes it...therefore, I've duplicated the form and with each referencing the specific module/sub.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Not completely sure if folks understand what you're trying to achieve.

If it helps, I've started a new project, added 2 modules and a userform, and put a label on the userform.

From the first module I've run this code to show the userrform modelessly (to enable other code to run whilst the userform's visible):
Code:
Sub shw()
UserForm1.Show modeless
End Sub

.............Then from the other module, I've run this code, and the label caption changes as expected:
Code:
Sub nme_lab()
UserForm1.Label1.Caption = "Hello"
End Sub

Is this what you were asking?
 
Upvote 0
I'll try to explain better. I have Module1 and Module2...both have a public variable 'answer'...which is filled by a Userform.

I've had to utilize/duplicate the userform and reference the variables specifically as "Module1.answer = " ....and "Module2.answer = " ...

Ideally, I'd like to have 1 UserForm that the 'answer' will work in both modules.
 
Upvote 0
Declaring two variables with the same name is asking for trouble.

Declare the variable AT THE TOP of just one of the modules, and ensure it's public - so it's available to all procedures in the project:
Code:
Public answer As String

I've now put a textbox on the userform, and coded the _change event to apply it's value to the variable:
Code:
Private Sub TextBox1_Change()
answer = Me.TextBox1.Value
End Sub
Then in module 1 I've put this:
Code:
Sub tst_mod_1()
MsgBox answer
End Sub

...and in module 2 I've put this:
Code:
Sub tst_mod_2()
MsgBox answer
End Sub
Use this in either module to start the userform:
Code:
Sub shw()
UserForm1.Show modeless
End Sub

..........and that should show you how to declare a global variable, apply a value to it from an open userform, and access the variable from anywhere in your project.

When the userform's active, and you've typed something into the textbox, run the msgbox code in both of the modules to test that they're both accessing your "answer" variable.

Does this help?
 
Upvote 0
Are module1 and module2 both normal modules?
Is answer declared with Dim or Public?

Alternatly, you could make "what the user entered into the form" a function.

If you have UserForm1 with a textbox and two command buttons, and code like this in the userform's code module

Code:
' code in Userform code module

Private Sub butOK_Click()
    Me.Hide
End Sub

Private Sub butCancel_Click()
    Unload Me
End Sub
You could put a function like this in a normal module and call that function from anywhere in the project.

Code:
' code in normal module

Function userFormsEntry(Optional defaultValue As String) As String
    With UserForm1
        .Caption = "Enter Data"
        .TextBox1 = defaultValue
        .Show
    End With
    
    userFormsEntry = UserForm1.TextBox1
    
    Unload UserForm1
End Function

Sub demo()
    Dim uiValue As String
    
    uiValue = userFormsEntry("my best guess")
    
    If uiValue = vbNullString Then
        MsgBox "cancel pressed"
    Else
        MsgBox "user entered " & uiValue
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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