VBA Userform ComboBox instantiation - excel

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.

Related

Use Variable from Useform in different Module

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.

Return a value from a userform

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.

Use checkbox dynamically added to userform

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

VBA: call Forms in sub

I have a big problem with getting called a UserForm from a sub.
This is a part of my code:
Sub TestForArray(ObjectName,FormName As String)
Forms(FormName).Controls(ObjectName).List = NewArr
End sub
when I call this sub in a Private Sub, like this
Private Sub UserForm_Initialize()
Call TestForArray("Form1", "ComboBox1")
End Sub
i get the error; "Sub or Function not defined" and higlights the word Forms
what am I doing wrong?
If the goal is to have one Sub that can be called from different UserForms to initialize the UserForm's Controls, you can give the UserForm itself as a parameter.
Example:
Sub TestForArray(oForm As UserForm, sObjectName As String)
aNewArr = [{1,2,3,4,5,6}]
oForm.Controls(sObjectName).List = aNewArr
End Sub
and:
Private Sub UserForm_Initialize()
Call TestForArray(Me, "ComboBox1")
End Sub
Greetings
Axel

How to use VBA to read the values of a checkbox from an Excel userform

I have created a userform that contains two checkboxes. I would like to be able to do different things depending on whether each box is checked or unchecked. However, it seems like no matter what I do, it will always tell me the original value of the checkboxes (false and false). Here is the code attached to clicking CommandButton1:
Private Sub CommandButton1_Click()
ReadData
End Sub
And here ReadData:
Sub ReadData()
Dim myForm As UserForm
Set myForm = UserForms.Add("ComplaintEntryForm")
Debug.Print (myForm!CheckBox1.Name)
Debug.Print (myForm!CheckBox1.Value)
Debug.Print (myForm!CheckBox2.Name)
Debug.Print (myForm!CheckBox2.Value)
End Sub
No matter how the boxes are checked, the immediate window always shows this:
VBA.UserForms.Add("ComplaintEntryForm").Show
CheckBox1
False
CheckBox2
False
I have a screenshot of the whole operation but it won't let me upload it because I'm a new user.
Try this method to load and show the form (this goes in a normal module):
Sub main()
Dim myForm As ComplaintEntryForm
Set myForm = New ComplaintEntryForm
myForm.Show
Set myForm = Nothing
End Sub
In the UserForm's own module, add the following:
Private Sub CheckBox1_Change()
readData
End Sub
Private Sub CheckBox2_Change()
readData
End Sub
Private Sub UserForm_Initialize()
Me.CheckBox1.Value = True
Me.CheckBox2.Value = False
End Sub
Private Sub readData()
Debug.Print Me.CheckBox1.Name
Debug.Print Me.CheckBox1.Value
Debug.Print Me.CheckBox2.Name
Debug.Print Me.CheckBox2.Value
End Sub
I've initialized the two checkboxes to specific values in the Initialize event. This means we are certain about the state the form will start in

Resources