Use checkbox dynamically added to userform - excel

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

Related

VBA -- Using a Userbox checkbox as show below, how do I set the value of a variable using a checkbox and then use that variable in a Module?

So currently, I have the following in a Userbox in VBA; a checkbox and a close window button.
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then vb1 = -1 Else vb1 = 1
End Sub
Private Sub CommandButton1_Click()
UserForm1.Hide
End Sub
And I have this in my Module.
Option Explicit
Sub GetRangeData_inputbox()
Dim vb1, vb2, vb3, vb4, vb5, vb6, vb7 As Integer
Range("c3") = vb1 * 1000
The primary purpose of the userbox is to check a box. I opted for a userbox since there are many of these boxes to be checked, but I only put one in for simplistic purposes.
The Goal of this is for a box to be checked, change vb1 to -1, or vb1 to 1 if the box is checked or unchecked, then, in the module, flow into a formula that does 1,000 (for example)*VB1.
Let me know,
Thanks!
To use a variable in a sub you must declare it like this Sub GetRangeData_inputbox(vb1 As Integer) and then you must call the sub in the CheckBox1_Click() event.
Here is a proof of concept:
Private Sub CheckBox1_Click()
Dim vb1 As Integer
If CheckBox1.Value = True Then vb1 = -1 Else vb1 = 1
Call GetRangeData_inputbox(vb1)
End Sub
And in the module:
Sub GetRangeData_inputbox(vb1 As Integer)
Range("c3") = vb1 * 1000
End Sub
Also you can replace UserForm1.Hide with UserForm1.Unload, unless you have a reason for hiding it.

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.

VBA Userform ComboBox instantiation

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.

How to make a variable public for all the workbook

I put this code into the module page
Option Explicit
Dim correct As Boolean
Sub setCorrect()
correct = True
End Sub
Sub checkCorrectTrue()
If correct Then
MsgBox "OK"
Else
MsgBox "NO"
End If
End Sub
Then when I call these 2 subs from a sheet my variable correct never switch to True
Private Sub CommandButton4_Click()
Call setCorrect
Call checkCorrectTrue
End Sub
Because you're using Dim correct As Boolean it is only available in that module. To be able to use the variable across modules you need to declare it using Public
Try using
Option Explicit
Public correct As Boolean
Sub setCorrect()
correct = True
End Sub
Sub checkCorrectTrue()
If correct Then
MsgBox "OK"
Else
MsgBox "NO"
End If
End Sub

VBA Pass Form Controls to Function

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.

Resources