2) Userform controls can be accessed from any module. The following code, when in a normal module, will set the intital value of Userform1's control ,TextBox1 and then show that user form.
Code:
Sub Test()
Userform1.TextBox1.Text = "Testing"
Userform1.Show
End Sub
3) Variables declared in a user form can be used elsewhere, if they are declared as Public. If this code is in the code module for Userform1. Note that the declaration of myVar is before any procedure code.
Code:
'in userform code module
Public myValue As Double
Private Sub TextBox1_Change()
' more code
It can be used in any module like this.
Code:
Sub test2()
UserForm1.myValue = 5
MsgBox UserForm1.myValue / 2
End Sub
A userform can been described as a class module with a "built-in" user interface. The coding technique above is similar to what one might find in the code module for a custom object (class module). One can use property procedures in a user form. Consider a user form with four controls, two text boxes and two command button and this code in its code module
Code:
'in userform code module
Property Get myValue() As Double
With Me
myValue = 10 * Val(.TextBox1.Text) + Val(.TextBox2.Text)
End With
End Property
Property Let myValue(inValue As Double)
With Me
.TextBox1 = Int(inValue / 10)
.TextBox2 = inValue Mod 10
End With
End Property
Private Sub CommandButton1_Click()
Me.Hide
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
That can be called by code like this, in a normal module
Code:
Sub test2()
With UserForm1: Rem creates one instance of uf1
.myValue = 345
.Show
MsgBox "first" & vbCr & .myValue & vbCr & .TextBox1.Text & ", " & .TextBox2.Text
End With
With UserForm1: Rem if hidden, same userform. if unloaded, different instance of uf1
MsgBox "second" & vbCr & .myValue & vbCr & .TextBox1.Text & ", " & .TextBox2.Text
End With
Unload UserForm1
End Sub
The .myValue property puts "34" in textbox1 and "5" in textbox2.
Note that the the difference between hiding and unloading a user form.
Also note that there can be different instances of a user form. Compare the results when pressing the Hide button with what happens when the Unload button is pressed.
Code:
Sub test2()
With UserForm1: Rem creates one instance of uf1
.myValue = 345
.Show
MsgBox "first" & vbCr & .myValue & vbCr & .TextBox1.Text & ", " & .TextBox2.Text
End With
With UserForm1: Rem if hidden, same userform. if unloaded, different instance of uf1
MsgBox "second" & vbCr & .myValue & vbCr & .TextBox1.Text & ", " & .TextBox2.Text
End With
Unload UserForm1
End Sub
QUESTION 1: Where to put code?
That's a question of personal style and preference.
I like to write UF code with the principles of self-contained and transferable.
In your situation, the task is to get data from the UF and put in in a cell.
"Self contained" means that the code that is necessary for the UF to function, the "what happens when the user changes a control" type code belongs in the UF code module.
If my project has the result of the UF put only in one cell, then that code would belong in the user form. This user form could be described as "Get data, put in cell"
If my project had the result in different cells in different situations, the "put in cell" code would go in a normal module (outside the UF code module). That would be a "get data" user form.
My style would be to avoid a "tell user form which cell, get data, put data in that cell" user form.
But this is all personal style. What works for you is what you should use.
I like user forms that can be exported from one project and used in another without having to remember to declare public variables in normal modules or ... other dependent outside coding.
Nomenclature: The technical term for where code resides is a "code module". A code module can be attached to an Excel object (user form, worksheet, workbook, etc), a user defended custom object (Class Module) or with no object (a normal module).
Also, the term "form" has many meanings, it can refer to a worksheet with specific cells for user entry. The term "userform" specifies that one is talking about a userform, not a worksheet that is used as a form.