Dynamic Checkbox select all - excel

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

Related

VBA Using Buttons Containing Pre-built Sentences to Build a Message About a Discussion with a Client

Complete noob here. I work for a company where I take phone calls all day and after completing the call I need to document my discussion. I find myself typing the same thing over and over and I'm trying to create a solution using VBA in Excel. I cannot utilize outside programs so I'm stuck on VBA. Below is my code. The goal is to allow a user to click various buttons to build a message covering the topics of the call. I'll then add the ability to copy the message which can then be pasted into the program we use for documenting notes.
As of current:
I'm able to pull from two columns in a table on excel which create buttons for each row (column 1) and when I hover over the buttons I can see the desired text (Column 2), however, clicking the buttons does not display text in the textbox.
Any assistance would be GREATLY appreciated!!
Class1
Option Explicit
Public WithEvents Btn As MSForms.CommandButton
Private Sub Btn_Click()
UserForm1.TextBox1.Text = UserForm1.TextBox1.Text & Btn.Tag & " "
End Sub
UserForm1
Private Sub UserForm_Click()
Option Explicit
Private Btns As Collection
Private Sub UserForm_Initialize()
Dim DataList As ListObject
Set DataList = ThisWorkbook.Worksheets("Sheet1").ListObjects("Table1")
Dim btnTop As Long, btnLeft As Long
Dim btnHeight As Long, btnWidth As Long
Dim btnSpace As Long
Dim ColCount As Long, CurrentCol As Long
btnTop = 6: btnLeft = 6
btnWidth = 72: btnHeight = 24
btnSpace = 6
ColCount = 4: CurrentCol = 1
Set Btns = New Collection
Dim cmdbtn As CommandButton
Dim btnEvent As Class1
Dim itm As Range
For Each itm In DataList.ListColumns(1).DataBodyRange
Set cmdbtn = Me.Controls("Frame1").Controls.Add("Forms.CommandButton.1")
With cmdbtn
.Top = btnTop
.Left = btnLeft
.Width = btnWidth
.Height = btnHeight
.Caption = itm
.Tag = itm.Offset(, 1)
.ControlTipText = .Tag
End With
Set btnEvnt = New Class1
Set btnEvnt.Btn = cmdbtn
Btns.Add btnEvnt
btnLeft = (btnSpace + btnWidth) * CurrentCol + btnSpace
If CurrentCol Mod ColCount = 0 Then
CurrentCol = 1
btnTop = btnSpace + btnTop + btnHeight
btnLeft = btnSpace
Else
CurrentCol = CurrentCol + 1
End If
Next itm
Me.Frame1.ScrollHeight = btnTop + btnHeight + btnSpace
End Sub

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 '<<<<<<<<

VBA obtaining checkbox values from userform

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.

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