Passing Userform values into another module

joobalooba

New Member
Joined
Jun 29, 2018
Messages
4
Hello,
I'm trying to set up a budget template for my colleagues. The first sheet is a set up sheet which contains a userform which they have to fill in. They then have to press a command button at the bottom called "Create New Budget".

Within the userform, there are several inputs for staff - e.g. "txtBudgetName" and "cboBudgetYear". Once staff have filled in these values and press the button, the sub attached to the command button has a Call "CreateBudget()" sub, which is saved in another module. The intention is that the CreateBudget sub creates the template budget sheet on another sheet.

The problem is that the CreateBudget sub relies on the staff inputs - txtBudgetName and cboBudgetYear etc, but I think these get forgotten once VBA moves off the userform and then onto the module with CreateBudget sub on it.

Is there something I can do to have VBA remember these values? Or should I just get rid of the CreateBudget sub and stuff all of its contents into the command button on the userform?

Thanks in advance!
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Within your standard module you declare your variables at the top of the module eg:

Code:
Public myText As String

Sub macro1()

MsgBox myText

End Sub

Then the userform module eg:

Code:
Private Sub UserForm_Click()

myText = TextBox1.Value
Call macro1

End Sub
 
Upvote 0
Are you unloading or hiding the useform when you 'move off' it?

If you are only hiding it then you can still access the values on it.
 
Upvote 0
Try
Code:
Sub macro1()
With Worksheets("Sheet2")
   .Range("B2").Value = UserForm1.txtbox1
   .Range("B4").Value = UserForm1.txtbox2
End With
End Sub
But I'd tend to keep it within the Button click event.
 
Upvote 0
You need to reference the userform to get the values from it.
Code:
Sub macro1()

    With Worksheets("Sheet2")
        .Range("B2").Value = UserForm1.txtbox1.Value
        .Range("B4").Value = UserForm1.txtbox2.Value
    End With

    Unload UserForm1

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,237
Messages
6,123,800
Members
449,127
Latest member
Cyko

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