VBA obtaining checkbox values from userform - excel

I'm having trouble obtaining values from checkboxes of a userform. The problem I have is that the userform creates a variable number of checkboxes based on a value from a sheet. The code for this:
Private Sub UserForm_Initialize()
Dim LastRow As Long
Dim i As Long
Dim Teller As Long
Dim chkBox As MSForms.CheckBox
Teller = 1
LastRow = Worksheets("Sheet").Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To LastRow
If Worksheets("Sheet").Cells(i, 1).Value = Worksheets("Sheet").Range("S1").Value Then
Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & Teller)
chkBox.Caption = Worksheets("Sheet").Cells(i, 9).Value
chkBox.Left = 5
chkBox.Top = 25 + ((Teller - 1) * 20)
Teller = Teller + 1
End If
Next i
End Sub
So this creates a number of checkboxes named CheckBox_1, CheckBox_2 etc.
The problem is when I try to get the value for CheckBox_1 in the module, CheckBox_1 has not yet been created so I'm not able to use it.
Dim x as String
With UserForm4
.Show
x = .CheckBox_1
MsgBox (x)
End
End With

You'll need to loop through .Controls the textbox is not a property on your form.

Related

Dynamically update the count of selected CheckBox in Excel using VBA

I am trying to find out a way to update the count of the selected checkboxes in excel using VBA.
i.e as the user selects the checkbox, the count has to get updated across the relevant filed. For example, If I select first check box ABC/18-49. The count at the top for (18-49) should get updated to 3.
P.S: This is how I have created the checkboxes dynamically.
Sub Main()
Dim Rng As Range
Dim WorkRng As Range
Dim Ws As Worksheet
On Error Resume Next
Set Ws = ThisWorkbook.Sheets(1)
Ws.Range("A:A").Insert
Set WorkRng = Ws.Range("A2:A" & Ws.UsedRange.Rows.Count)
Application.ScreenUpdating = False
For Each Rng In WorkRng
With Ws.CheckBoxes.Add(Rng.Left, Rng.Top, Rng.Width, Rng.Height)
.Characters.Text = "Yes"
End With
Next
WorkRng.ClearContents
WorkRng.Select
Application.ScreenUpdating = True
End Sub
Try the next way, please:
Copy the next Subs in a standard module and run the first one. It will assign a specific macro to all check boxes from column A:A:
Sub AssingMacro()
Dim sh As Worksheet, s As Shape, chkB As CheckBox
Set sh = ActiveSheet
For Each s In sh.Shapes
If left(s.Name, 6) = "Check " And s.TopLeftCell.Column = 1 Then
s.OnAction = "CheckBoxesHeaven"
End If
Next
End Sub
Sub CheckBoxesHeaven()
Dim sh As Worksheet, chB As CheckBox
Set sh = ActiveSheet
Set chB = sh.CheckBoxes(Application.Caller)
If chB.Value = 1 Then
Debug.Print chB.TopLeftCell.Offset(0, 2).Value
If chB.TopLeftCell.Offset(0, 2).Value = "18-49" Then
sh.Range("C3").Value = sh.Range("C3").Value + 1
ElseIf chB.TopLeftCell.Offset(0, 2).Value = "50-64" Then
sh.Range("C1").Value = sh.Range("C1").Value + 1
Else
sh.Range("C2").Value = sh.Range("C2").Value + 1
End If
Else
If chB.TopLeftCell.Offset(0, 2).Value = "18-49" Then
sh.Range("C3").Value = sh.Range("C3").Value - 1
ElseIf chB.TopLeftCell.Offset(0, 2).Value = "50-64" Then
sh.Range("C1").Value = sh.Range("C1").Value - 1
Else
sh.Range("C2").Value = sh.Range("C2").Value - 1
End If
End If
End Sub
Assort the values in range "C1:C3" to match the appropriate check boxes value. In order to automatically do that, please use the next code:
Sub ResetCheckBoxesValues()
Dim sh As Worksheet, chkB As CheckBox, i As Long
Dim V50_64 As Long, V18_49 As Long, VLess18 As Long
Set sh = ActiveSheet
For Each chkB In sh.CheckBoxes
If chkB.TopLeftCell.Column = 1 Then
Select Case chkB.TopLeftCell.Offset(0, 2).Value
Case "50-64"
If chkB.Value = 1 Then V50_64 = V50_64 + 1
Case "18-49":
If chkB.Value = 1 Then V18_49 = V18_49 + 1
Case "<18":
If chkB.Value = 1 Then VLess18 = VLess18 + 1
End Select
End If
Next
sh.Range("C1:C3").Value = Application.Transpose(Array(V50_64, VLess18, V18_49))
End Sub
Start playing with check boxes selection. It will add a unit to the appropriate cell if checking and decrease it with a unit in case of unchecking.
Please, test it and send some feedback
It will not be "very" dynamic, make sure to click on a random Excel cell, to make the formula recalculate after updating the check on the checkbox.
But the formula works in Excel, with the checkboxes you have created:
Public Function CountCheckBoxes()
Dim chkBox As Shape
Dim counter As Long
With ThisWorkbook.Worksheets(1)
For Each chkBox In .Shapes
If InStr(1, chkBox.Name, "Check Box") Then
If .Shapes(chkBox.Name).OLEFormat.Object.Value = 1 Then
counter = counter + 1
End If
End If
Next chkBox
End With
CountCheckBoxes = counter
End Function
Probably you should think about a suitable workaround to avoid ThisWorkbook.Worksheets(1), depending on where the code is residing.

Return the value from a checklist on a UserForm

This makes a UserForm checklist and it works.
Sub UserForm_Initialize()
Dim LastRow As Long
Dim i As Long
Dim chkBox As MSForms.CheckBox
LastRow = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To LastRow
Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & i)
chkBox.Caption = Worksheets("Sheet1").Cells(i, 1).Value
chkBox.Left = 5
chkBox.Top = 5 + ((i - 1) * 20)
Next i
End Sub
I want to select as many of the boxes as I need, hit a command button and have the resulting values pasted in a different cell.
For example I have 1-10 in cells A1-A10. The first piece makes a checklist for each value 1-10. If I check the boxes next to 2, 3, 5, and 7, hit the command button and then want 2, 3, 5, and 7 to be entered into cells G2, G3, G5 and G7.
I cant figure out how to get this last part to happen. I have tried to make If statement
Sub CommandButton1_Click()
If chkBox1 = False Then GoTo Here
Else
Range("G1").Value = Me.TextBox1.Text
End If
Here
End Sub
I get
"Compile error: Variable not defined"
I tried different names instead of "chkbox1" but get the same error.
I think this does what you want.
You can reference the controls using their name and loop through them in a similar way to the Initialize code. By declaring 'LastRow` before the sub we can use it in both subs.
Dim LastRow As Long
Private Sub CommandButton1_Click()
Dim i As Long
For i = 1 To LastRow
If Me.Controls("CheckBox_" & i) Then
Range("G" & i).Value = i
End If
Next i
End Sub
Sub UserForm_Initialize()
Dim i As Long
Dim chkBox As MSForms.CheckBox
LastRow = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To LastRow
Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & i)
chkBox.Caption = Worksheets("Sheet1").Cells(i, 1).Value
chkBox.Left = 5
chkBox.Top = 5 + ((i - 1) * 20)
Next i
End Sub

Get and write a value to a sheet from a textbox created dynamically in a UserForm

I have a UserForm composed of a TextBox and a CommandButton.
By entering a value in the TextBox, for example 3, and clicking the CommandButton, I create three new TextBox with labels linked to each one of them. It also creates just under those TextBox and Labels another CommandButton.
I write in each new TextBox a value. For example in the first TextBox I write "Banana", in the second "Apple" and so on.
I want by clicking the new CommandButton, to get the value from the new TextBoxes and write them in a sheet.
How do I get the value from these new TextBoxes considering they are created during the runtine.
Here is the code linked to my UserForm:
Option Explicit
Dim cmdArray() As New Classe1
Public Sub nbEquipButtonValidation_Click()
Dim i As Variant
Dim Nb_equip As Integer
Dim j As Long
Nb_equip = UserForm1.nbEquipTextBox.Value
For i = 1 To Nb_equip
Dim EquipLabel
Dim Text_Boxes
Dim CmdBtn
Set EquipLabel = Me.Controls.Add("Forms.Label.1")
With EquipLabel
.Top = 25 + 10 * i * 2
.Left = 10
.Caption = "Equipement n°" & CStr(i)
.Name = "Equip" & CStr(i)
End With
Set Text_Boxes = Me.Controls.Add("Forms.TextBox.1", True)
With Text_Boxes
.Top = 20 + 10 * i * 2.1
.Left = 100
.Name = "Text_Box" & CStr(i)
End With
Next i
Set CmdBtn = Me.Controls.Add("Forms.CommandButton.1")
With CmdBtn
.Top = 20 + 10 * Nb_equip * 2.1 + 30
.Left = 75
.Caption = "Créer"
.Name = "Validation"
End With
' Apply a class to the new Button
j = 1
ReDim Preserve cmdArray(1 To j)
Set cmdArray(j).CmdEvents = CmdBtn
Set CmdBtn = Nothing
End Sub
Here is the class I created to get these data and write them in a sheet by clicking the second CommandButton.
Option Explicit
Public WithEvents CmdEvents As MSForms.CommandButton
Private Sub CmdEvents_Click()
Dim Ws As Worksheet
Set Ws = Worksheets("Sheet1")
Dim i As Variant
Dim Nb_equip As Integer
Nb_equip = UserForm1.nbEquipTextBox.Value
For i = 1 To Nb_equip
With Ws
.Cells(6, 2 + i * 2).Value = "Exp" & CStr(i)
End With
Next i
End Sub
In my loop I want to replace "Exp" & CStr(i) by the name of the value in the TextBox according to the position i of creation.
This should work:
Modify your class to add a reference to the Form object:
Option Explicit
Public WithEvents CmdEvents As MSForms.CommandButton
Public frm As Object '<<<<<<<<<<<
Private Sub CmdEvents_Click()
Dim Ws As Worksheet
Set Ws = Worksheets("Sheet1")
Dim i As Variant
Dim Nb_equip As Integer
Nb_equip = frm.nbEquipTextBox.Value
For i = 1 To Nb_equip
Ws.Cells(6, 2 + i * 2).Value = frm.Controls("Exp" & CStr(i)).Text
Next i
End Sub
Then add the line below:
Set cmdArray(j).CmdEvents = CmdBtn
Set cmdArray(j).frm = Me '<<<<<<<<

Dynamic Checkbox select all

I have created a userform with a dynamic checkbox using the below code. I am struggling to figure out how to add a select all/unselect all checkbox at the bottom (or anywhere) since the list is dynamic. Any assistance will be appreciated.
Private Sub UserForm_Initialize()
'Create Checkboxes form with director names
Dim lRow As Long
Dim i As Long
Dim chkBox As MSForms.CheckBox
lRow = Cells(Rows.Count, 2).End(xlUp).Row
Sheets("Directors_Database").Activate
For i = 4 To lRow
Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & i)
chkBox.Caption = Worksheets("Directors_Database").Cells(i, 2).Value
chkBox.Left = 5
chkBox.Top = 5 + ((i - 2) * 20)
chkBox.Width = 350
Next i
Set chkBox = Nothing
End Sub
You should create the "select all" checkbox at design time, not at runtime - else it getting a little tricky to assign a Event-Routine (see this discussion)
You could place the SelectAll-checkBox at the bottom by setting the top property (as you do already).
Then assign an Click-Event to that Checkbox:
Private Sub CheckBoxSelectAll_Click()
Dim c As Control
For Each c In Me.Controls
If TypeOf c Is MSForms.CheckBox Then
c.Value = CheckBoxSelectAll.Value
End If
Next c
End Sub

vba dynamically created checkboxes onclick events

I have a list of checkboxes on a userform created from a list on an Excel sheet.
I want to have an additional checkbox that when checked, is testing the caption value of all the checkboxes in the list. If there is a match to a target string, these checkboxes should check as well.
I can create the list but referencing the checkboxes after creation and triggering events is the issue. So far this is all I have:
Private Sub UserForm_Initialize()
Dim curColumn As Long
Dim LastRow As Long
Dim i As Long
Dim chkBox As MSForms.CheckBox
curColumn = 1 'column index
LastRow = Worksheets("Parts").Cells(Rows.Count, curColumn).End(xlUp).Row
For i = 2 To LastRow
Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & i)
chkBox.Caption = Worksheets("Parts").Cells(i, curColumn).Value
chkBox.Left = 5
chkBox.Top = 25 + ((i - 1) * 20)
chkBox.Width = 200
Next i
End Sub

Resources