View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Peter T[_5_] Peter T[_5_] is offline
external usenet poster
 
Posts: 84
Default VBE tricky question

Not possible to do what you're doing, ie event code to the form duiring
runtime from itself. But there's a much easier way, try this

Add a Class module named
Paste the following into the class module and form module as indicated
Run the form and press click some boxes

' code in class named "clsChkBoxEvents"
Public WithEvents cbx As MSForms.CheckBox
Public idxR As Long, idxC As Long

Private Sub cbx_Change()
If cbx Then
cbx.Parent.ShowFail idxR, idxC
Else

cbx.Parent.HideFail idxR, idxC
End If

End Sub
''' end class clsChkBoxEvents

' code in a userform

Private maCBX(1 To 20) As clsChkBoxEvents
Private Sub UserForm_Initialize()
Dim i As Long, j As Long, n As Long
Dim ctr As MSForms.Control
Const cWd As Single = 45, cHt As Single = 18

For i = 0 To 4
For j = 0 To 3
Set ctr = Me.Controls.Add("Forms.Checkbox.1")
With ctr
.Left = 3 + j * (cWd + 9)
.Top = 3 + i * (cHt + 9)
.Width = cWd
.Height = cHt
.Caption = "CB " & i + 1 & " : " & j + 1
End With
n = n + 1
Set maCBX(n) = New clsChkBoxEvents
Set maCBX(n).cbx = ctr
maCBX(n).idxR = i + 1
maCBX(n).idxC = j + 1
Next
Next
End Sub

Public Sub ShowFail(r As Long, c As Long)
MsgBox "ShowFail " & r & " " & c
End Sub
Public Sub HideFail(r As Long, c As Long)
MsgBox "HideFail " & r & " " & c
End Sub

''''end userform code

Regards,
Peter T