I couldn't quite find what I'm looking for but maybe you can help me anyway.
My problem is that I have a userform where the user has to make an input. I want to store that input and use it later in a different module i.e. paste it into a cell. The simple solution should be to just make it a public variable, but for some reason it won't work. Here is the code I tried to use:
Userform:
Option Explicit
Public VarBezeichnungReifenliste As String
Private Sub CommandButton3_Click()
VarBezeichnungReifenliste = TextBox1.Value
Call Übertragen
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
Module:
Option Explicit
Public Sub Übertragen()
Worksheets("XY").Cells(1, 1).Value = VarBezeichnungReifenliste
End Sub
The error message says the variable is not declared (VarBezeichnungReifenliste) so i guess I didn't declare it publicly enough?
The userform itself is opened via a simple button on the worksheet using Userform1.Show. So nothing fancy here.
Publicly Enough
Solution1
UserForm1:
Option Explicit
Private Sub CommandButton3_Click()
VarBezeichnungReifenliste = TextBox1.Value
Module1.Übertragen
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
Module1:
Option Explicit
Public VarBezeichnungReifenliste As String
Sub Übertragen()
Worksheets("XY").Cells(1, 1).Value = VarBezeichnungReifenliste
End Sub
Conclusion
Just move the variable declaration
Public VarBezeichnungReifenliste As String
to a 'normal' module.
Solution2
UserForm1:
Option Explicit
Public VarBezeichnungReifenliste As String
Private Sub CommandButton3_Click()
VarBezeichnungReifenliste = TextBox1.Value
Module1.Übertragen
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
Module1:
Option Explicit
Sub Übertragen()
Worksheets("XY").Cells(1, 1).Value = UserForm1.VarBezeichnungReifenliste
Worksheets("XY").Cells(1, 1).Select
End Sub
Conclusion
Just use
Worksheets("XY").Cells(1, 1).Value = UserForm1.VarBezeichnungReifenliste
instead of
Worksheets("XY").Cells(1, 1).Value = VarBezeichnungReifenliste
in Module1.
Solution3
UserForm1:
Option Explicit
Public VarBezeichnungReifenliste As String
Private Sub CommandButton3_Click()
VarBezeichnungReifenliste = TextBox1.Value
Übertragen
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
Sub Übertragen()
Worksheets("XY").Cells(1, 1).Value = VarBezeichnungReifenliste
End Sub
Conclusion
Move everything into UserForm1.
VBA is weird about storing variables over the long-term. As a general rule of thumb, if you're able to interact with Excel workbooks in between a variable being saved and when you need to get the value, you can't count on that variable still holding its value.
The safest way to get around this is to just store your value in a cell of a hidden worksheet, and read it from there when you need it.
Related
I am trying to return a value from a userform to another macro.
Here is an example of a piece of code that I want to return the value intMonth:
sub comparison()
UserForm1.Show
end sub
then I have the userform code:
Private Sub initialize()
OptionButton1 = False
End Sub
Private Sub OptionButton1_Click()
intMonth = 1
Me.Hide
End Sub
How do I get the intMonth value of 1 back to my original comparison() function?
This is a minimal example, but should help.
In the UserForm:
Option Explicit
Option Base 0
Public intMonth As Long ' <-- the variable that will hold your output
Private Sub initialize()
OptionButton1 = False
intMonth = 0
End Sub
Private Sub CommandButton1_Click() ' OK button
Me.Hide
End Sub
Private Sub OptionButton1_Click()
intMonth = 1 '<-- set the value corresponding to the selected radio button
End Sub
Private Sub OptionButton2_Click()
intMonth = 2
End Sub
In a module or ThisWorkbook:
Option Explicit
Option Base 0
Sub comparison()
UserForm1.Show
MsgBox CStr(UserForm1.intMonth) ' <-- retrieve the value
End Sub
Another useful way to achieve what you need is to wrap the code in a public function in the userform.
In the UserForm:
Option Explicit
Option Base 0
Private intMonth As Long
Public Function Choose_Option()
OptionButton1 = False
intMonth = 0
Me.show()
Choose_Option = intMonth
End sub
Private Sub CommandButton1_Click() ' OK button
Me.Hide
End Sub
Private Sub OptionButton1_Click()
intMonth = 1
End Sub
Private Sub OptionButton2_Click()
intMonth = 2
End Sub
Then in module, it is simple as this:
Option Explicit
Option Base 0
Sub comparison()
MsgBox Userform1.Choose_Option()
End Sub
This way, the function is in charge of showing the userform, prompting the user and returning the value.
If you debug this function, you will see that after Me.Show() is called, the function halts and continue only when the userform is hidden, which is done in the OK button.
I add CheckBox1 to my UserForm with this code:
Private Sub UserForm_Initialize()
Dim opt As Variant
Set opt = UserForm1.Controls.Add("Forms.checkbox.1", "CheckBox1", True)
End Sub
Now when I click on a CommandButton I want to Check if the CheckBox1 is checked or not:
Private Sub CommandButton1_Click()
If CheckBox1.Value = False Then
MsgBox "F"
End If
End Sub
But this code doesn't work; I think because the check box is added dynamically.
This is just a simplification of the code for solving the problem.
This is what you are thinking of:
Option Explicit
Private Sub UserForm_Initialize()
Dim opt As Variant
Set opt = Me.Controls.Add("Forms.checkbox.1", "CheckBox1", True)
End Sub
Private Sub CommandButton1_Click()
If Not Me.Controls("CheckBox1") Then
MsgBox "F"
End If
End Sub
However, depending on your experience and desires to write better code, you may decide to follow some MVC pattern in working with Forms. Read these for some more ideas about it:
https://codereview.stackexchange.com/questions/154401/handling-dialog-closure-in-a-vba-user-form
http://www.vitoshacademy.com/vba-the-perfect-userform-in-vba/ (Disclaimer - this is my blog)
https://rubberduckvba.wordpress.com/2017/10/25/userform1-show/
It will need to be as follows
Private Sub CommandButton1_Click()
If Me.Controls("Checkbox1").Value = False Then
MsgBox "F"
End If
End Sub
I have a Problem with a Userform which I called "ComboTest2". It only consists of two Comboboxes. If I instantiate the USerform as an object then the following Code doesn't work in the sense that the second combobox of the Userform doesn't contain the desired data.
Sub FillCombo(ByVal row As Long)
Dim rgCities As Range
Set rgCities = Worksheets("Tabelle2").Range("B2:D2").Offset(row)
ComboTest2.ComboBox2.Clear
ComboTest2.ComboBox2.List = WorksheetFunction.Transpose(rgCities)
ComboTest2.ComboBox2.ListIndex = 0
End Sub
Sub FillMain()
Dim ComboForm2 As ComboTest2
Set ComboForm2 = New ComboTest2
ComboForm2.Show
End Sub
UserForm-Code:
Private Sub CommandButton1_Click()
Me.Hide
End Sub
Private Sub CommandButton2_Click()
Me.Hide
End Sub
Private Sub ComboBox1_Change()
FillCombo ComboBox1.ListIndex
End Sub
Private Sub UserForm_Initialize()
ComboBox1.List = Worksheets("Tabelle2").Range("A2:A5").Value
ComboBox1.ListIndex = 0
FillCombo ComboBox1.ListIndex
End Sub
But if I use the "default instantiation" by VBA which means that I change the FillMain Sub to:
Sub FillMain2()
Dim ComboForm2 As ComboTest2
Set ComboForm2 = New ComboTest2
'ComboForm2.Show
ComboTest2.Show
End Sub
Then everything is fine. Why is that so?
Best regards
It's because FillCombo is referring to the userform by name, and therefore to the default instance (you're actually creating a new instance of the form). If that code is not in the userform, and I'm not sure why you would have it outside the form, you should pass the combobox as an argument to it.
All,
I have been struggling with this for a while: is it possible to pass an object to a function?
Here is what I am trying to accomplish:
Get the name of which control was pressed on a form (as object?)
Send the control's name to function "MyFunction" (as reference?)
Disable that same control on "MyFunction"
Called from form1:
Private Sub button1_Click()
Dim caller As String
caller = Form1.ActiveControl.Name
MyFunction(caller)
End Sub 'I'm able to pass it as a string
button1_Click calls MyFunction and passes caller to it:
Private Sub MyFunction(caller As String)
caller.Enabled = False
End Sub
I understand this will not work as a string. How could I possibly do it as an actual object?
Thank you!
There is little problem passing an object to a sub:
Private Sub Disable(c As Control)
MsgBox c.Name
c.Enabled = False
End Sub
Private Sub CommandButton1_Click()
Disable CommandButton1
End Sub
Private Sub CommandButton2_Click()
Disable CommandButton2
End Sub
Private Sub CommandButton3_Click()
Disable CommandButton3
End Sub
In the above I created a userform with three buttons, they say who they are when clicked and are then disabled.
Note that
Disable CommandButton1
can be replaced by
Disable Me.ActiveControl
or even just
Disable ActiveControl
You can even use Variant like so (rough example):
Private Sub CommandButton1_Click()
EnableDisable ActiveControl, "disable"
End Sub
Private Sub EnableDisable(control As Variant, status As String)
If status = "enabled" Then
control.Enabled = True
Else
control.Enabled = False
End If
End Sub
John Coleman's example is better than mine, though.
I want to pass a value selected by user to be displayed in MsgBox. I write the following code but its display nothing.
Public Sub CommandButton1_Click()
SelectedCity = Me.ComboBox1.Value
DistSystem
End Sub
Sub DistSystem()
MsgBox (SelctedCity)
End Sub
Second procedure cannot read variable because of wrong scope.
You have to declare SelectedCity variable as global:
Global SelectedCity
Public Sub CommandButton1_Click()
SelectedCity = Me.ComboBox1.Value
DistSystem
End Sub
Sub DistSystem()
MsgBox (SelctedCity)
End Sub
There are times when you can't pass it in an argument, but you can pass it here.
Option Explicit 'forces to declare all variables
Public Sub CommandButton1_Click()
call DistSystem (Me.ComboBox1.Value)
'same as ( without call, and () ) :
' DistSystem Me.ComboBox1.Value
End Sub
Sub DistSystem(byval selectedCity$) 'same as: byval SelectedCity as string
MsgBox SelectedCity
End Sub
This works even if DistSystem is in a different module.
Declare Selectedcity as a public OUTSIDE of the Sub
Public SelectedCity as String
Public Sub CommandButton1_Click()
SelectedCity = Me.ComboBox1.Value
Call DistSystem
End Sub
Sub DistSystem()
MsgBox (SelectedCity)
End Sub
Obviously place DistSystem in a module!