gravanoc
Active Member
- Joined
- Oct 20, 2015
- Messages
- 351
- Office Version
- 365
- Platform
- Windows
- Mobile
I would like to dynamically create some checkboxes and then associate events with them. They will be in a collection.
I have a class module named cCheckBoxHandler with this code:
I'll add the specifics for the event once I get the rest working. This class is referenced in another script. The problem occurs when trying to assign the newly created checkbox's variable to the class instance's variable.
That line of code is below in bold & comments. There is a type mismatch error, but I'm not sure what I should be using as the common type.
I have a class module named cCheckBoxHandler with this code:
VBA Code:
Public WithEvents chkBox As MSForms.CheckBox
Private Sub chkBox_Click()
End Sub
I'll add the specifics for the event once I get the rest working. This class is referenced in another script. The problem occurs when trying to assign the newly created checkbox's variable to the class instance's variable.
That line of code is below in bold & comments. There is a type mismatch error, but I'm not sure what I should be using as the common type.
VBA Code:
Dim collChecks As Collection
Sub CreateCheckBoxes()
'Declare variables
Dim c As Range
Dim chkBox As Control
Dim ansBoxDefault As Long
Dim chkBoxRange As Range
Dim chkBoxDefault As Boolean
Dim mSht As Worksheet
Dim cBoxH As cCheckBoxHandler
Set collChecks = New Collection
Set mSht = Sheets("Master")
On Error Resume Next
Set chkBoxRange = Application.InputBox(Prompt:="Select cell range", _
Title:="Create checkboxes", Type:=8)
If Err.Number <> 0 Then Exit Sub
ansBoxDefault = MsgBox("Should the boxes be checked?", vbYesNoCancel, _
"Create checkboxes")
If ansBoxDefault = vbYes Then chkBoxDefault = True
If ansBoxDefault = vbNo Then chkBoxDefault = False
If ansBoxDefault = vbCancel Then Exit Sub
On Error GoTo 0
For Each c In chkBoxRange
Set chkBox = chkBoxRange.Parent.Checkboxes.Add(0, 1, 1, 0)
Set cBoxH = New cCheckBoxHandler
' ------ ERROR OCCURS BELOW---------'
[B] Set cBoxH.chkBox = chkBox[/B]
'-------ERROR OCCURS ABOVE--------'
collChecks.Add cBoxH
With chkBox
.Top = c.Top + c.Height / 2 - chkBox.Height / 2
.Left = c.Left + c.Width / 2 - chkBox.Width / 2
.Name = c.Address
.LinkedCell = c.offset(0, 0).Address(external:=True)
.Locked = False
.Caption = ""
End With
c.Value = chkBoxDefault
c.NumberFormat = ";;;"
Next c
End Sub