
Displaying Another UserForm when click on CheckBox in UserForm
Cynthia
Here's an example to get you started. It uses the technique from John's
website. First, create a class module with this in it
Public WithEvents ChBoxGrp As MSForms.CheckBox
Private Sub ChBoxGrp_Click()
UserForm2.Show
End Sub
Then in the initialize event of your userform, put this. Notice that as I'm
adding controls to the userform, I'm also adding them to the class so that
the Click event defined in the class will run when any of them is clicked.
I also used a For-Each loop instead of a Do Loop. You can still use the Do
Loop, I didn't because I prefer the For Each.
Option Explicit
Dim ChBoxes() As New Class1
Private Sub UserForm_Initialize()
Dim BoxCount As Long
Dim ws As Worksheet
Dim cell As Range
Dim ctl As Control
Set ws = ThisWorkbook.Sheets("Candidates")
For Each cell In ws.Range("A1", ws.Range("a1").End(xlDown))
Set ctl = Me.Controls.Add("Forms.CheckBox.1")
With ctl
.Caption = cell.Offset(0, 1).Value
cell.Offset(0, 12).Value = False
.ControlSource = ws.Name & "!" & _
cell.Offset(0, 12).Address
.Top = 18 * cell.Row
.Left = 8
'Add the control to the class
BoxCount = BoxCount + 1
ReDim Preserve ChBoxes(1 To BoxCount)
Set ChBoxes(BoxCount).ChBoxGrp = ctl
End With
Next cell
End Sub
--
{*filter*} Kusleika
MVP - Excel
Post all replies to the newsgroup.
Quote:
> I cannot seem to get another userform to load when the user clicks on one
of
> the checkboxes created from the code below. I have tried several options
> and have two of them pasted at the bottom of this message. I have also
> tried the http://www.*-*-*.com/
web
> site. That does not seem to work either. Please let me know what I am
> doing wrong. Any help is greatly appreciated! And as always thanks in
> advance.
> Option Explicit
> Dim WS As Worksheet
> Dim rw As Integer
> Dim x As MSForms.Control
> Private Sub CommandButton1_Click()
> Me.Hide
> Unload Me
> End Sub
> Private Sub UserForm_Initialize()
> Dim i As Integer
> i = 1
> Set WS = Worksheets("Candidates")
> With WS
> rw = 2
> Do While .Cells(rw, 1) <> ""
> Set x = Me.Controls.Add("Forms.CheckBox.1")
> x.Caption = .Cells(rw, 2)
> .Cells(rw, 13) = False
> x.ControlSource = "candidates!M" & rw
> x.Top = 18 * rw
> x.Left = 8
> rw = rw + 1
> Loop
> End With
> End Sub
> Here is where my problem occurs. This code is just ignored. What I am
> trying to accomplish is displaying another userform when the user clicks
on
> one of the checkboxes created from the above code.
> Private Sub x_Click()
> Dim cntrl As MSForms.Control
> Dim i As Integer
> i = 1
> For Each x In FCandidates.Controls
> If TypeOf x Is MSForms.CheckBox Then
> If x.Value = True Then
> Load FCanChoice
> FCanChoice.Show
> i = i + 1
> End If
> End If
> Next
> End Sub
> Here is another one of my examples that I have tried.
> Private Sub x_Click()
> If x = True Then
> Load FCanChoice
> FCanChoice.Show
> Else
> 'Do Nothing
> End If
> End Sub
> Thanks again,
> Cynthia G.