I'm trying to clean up my spaghetti into a single sub that will handle all checkbox change event on a worksheet. It helps me consolidate the changes to one sub for changes when we adjust rates, it looks better, and looks more professional (my hope).
This is the functioning code block that I have copied 15 checkbox change events:
Private Sub ChkDetailStnd_Change()
FastON
With Me.TxtDetail
If .Enabled = True Then
Me.ChkDetailStnd.value = True
.Enabled = False
.Locked = False
.BackStyle = fmBackStyleOpaque
.BackColor = &HE0E0E0
.value = 10
Else
Me.ChkDetailStnd.value = False
.Enabled = True
.Locked = True
.BackStyle = fmBackStyleTransparent
.BackColor = &HFFFFFF
End If
End With
FastOFF
End Sub
This is what I am trying to get working by cycling through the controls on the worksheet. I am getting errors in how I refer to the control in the with statement and then changing the checkbox value.
Sub SumPgChk(ChkName As String)
Dim ctrl As OLEObject
Dim ctrls As OLEObjects
Dim i As String
Dim TxtName As String
Dim dict As New Dictionary
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Summary Page")
dict.Add Key:="TxtB10", item:=15
dict.Add Key:="TxtB15", item:=15
dict.Add Key:="TxtB20", item:=15
dict.Add Key:="TxtB25", item:=15
dict.Add Key:="TxtB30", item:=15
dict.Add Key:="TxtB35", item:=15
dict.Add Key:="TxtB45", item:=15
dict.Add Key:="TxtB55", item:=15
dict.Add Key:="TxtAdmin", item:=5
dict.Add Key:="TxtDetail", item:=10
dict.Add Key:="TxtDelivery", item:=5
dict.Add Key:="TxtMarkup", item:=5
dict.Add Key:="TxtInstall", item:=5
For Each ctrl In ws.OLEObjects
If ctrl.name = ChkName Then
TxtName = Mid(ChkName, 4, Len(ChkName) - 7)
TxtName = "Txt" & TxtName
With ctrls(TxtName)
If .Enabled = True Then
ActiveSheet.OLEObjects(ChkName).Object.value = True
.Enabled = False
.Locked = False
.BackStyle = fmBackStyleOpaque
.BackColor = &HE0E0E0
.value = dict(TxtName)
Else
ctrl(ChkName).Object.value = False
.Enabled = True
.Locked = True
.BackStyle = fmBackStyleTransparent
.BackColor = &HFFFFFF
End If
End With
End If
Next ctrl
Set dict = Nothing
End Sub
I've tried addressing the checkboxes a few different ways based on a few forums and Microsoft directly. Any help would really be appreciated.
