Why Checkbox count = 4

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

attempting to better understand the use of TypeOf I have created:
All message boxs return the proper number, except the CkBoxct,
which returns 4, when IN FACT there is only 1 Checkbox on my Userform1.
Any ideas why this problem?
TIA,
Jim May

Private Sub CommandButton1_Click()
For Each ctl In UserForm1.Controls
If TypeOf ctl Is MSForms.Label Then labelct = labelct + 1
If TypeOf ctl Is MSForms.Frame Then Framect = Framect + 1
If TypeOf ctl Is MSForms.CheckBox Then CkBoxct = CkBoxct + 1
If TypeOf ctl Is MSForms.TextBox Then Txtct = Txtct + 1
If TypeOf ctl Is MSForms.OptionButton Then OptBnct = OptBnct + 1
If TypeOf ctl Is MSForms.ComboBox Then ComBoxct = ComBoxct + 1
Next ctl
MsgBox "labels total " & labelct
MsgBox "Frame total " & Framect
MsgBox "Checkboxes total " & CkBoxct
MsgBox "Textboxs total " & Txtct
MsgBox "OptionButtons total " & OptBnct
MsgBox "ComboBoxs total " & ComBoxct
End Sub
 
Your code worked fine for me. Are you sure you don't have Checkbox's
lying on top of one another such that it appears there is only one?

Hth,
Merjet
 
At the risk of offending, are you sure that there aren't checkboxes
hidden by other controls (perhaps frame(s)?)?

If you execute

?UserForm1.Controls.Count

in the immediate window, do you get the correct total?
 
JE,
When I (in the immediate window) enter your suggested

?UserForm1.Controls.Count

I get the right total count -- But still a Checkbox Overcount;
I'm still working on.. I'll report back if I CRACK..

No Checkboxes ARE HIDDEN

Tks,

Jim
 
Add a debugging statement

Private Sub CommandButton1_Click()
For Each ctl In UserForm1.Controls
If TypeOf ctl Is MSForms.Label Then labelct = labelct + 1
If TypeOf ctl Is MSForms.Frame Then Framect = Framect + 1
If TypeOf ctl Is MSForms.CheckBox Then
CkBoxct = CkBoxct + 1
debug.print CkBoxct, ctl.name
end if
If TypeOf ctl Is MSForms.TextBox Then Txtct = Txtct + 1
If TypeOf ctl Is MSForms.OptionButton Then OptBnct = OptBnct + 1
If TypeOf ctl Is MSForms.ComboBox Then ComBoxct = ComBoxct + 1
Next ctl
MsgBox "labels total " & labelct
MsgBox "Frame total " & Framect
MsgBox "Checkboxes total " & CkBoxct
MsgBox "Textboxs total " & Txtct
MsgBox "OptionButtons total " & OptBnct
MsgBox "ComboBoxs total " & ComBoxct
End Sub


See the results in the immediate window in the VBE (view=>Immediate window
if it isn't already visible)
 
Thanks Tom:

Your code produces in my immediate window:

1 OptionButton1
2 OptionButton2
3 CheckBox1

And my Msgbox for Checkboxes indicates 3;

But there is ONLY 1 !!
 
My ONLY two OptionButtons ARE INSIDE my Frame Control
With my Userform active, when I click on the Properties Drop-down
I do not see my 2 optionbuttons,,, hummmmmmm -- these are
being counted as a checkbox (no doubt) .... hummmm
 
I affirm your experience. I put 2 OptionButtons in a Frame and they
were miscounted as CheckBoxes. However, using the following gave the
correct count.

For Each ctl In UserForm1.Controls
If TypeName(ctl) = "CheckBox" Then CkBoxct = CkBoxct + 1
Next ctl

Hth,
Merjet
 
Yeah, The following works fine -- WITHOUT A PROBLEM

Thanks for your input..

Private Sub CommandButton2_Click()
For Each ctl In UserForm1.Controls
If TypeName(ctl) = "Label" Then labelct = labelct + 1
If TypeName(ctl) = "Frame" Then Framect = Framect + 1
If TypeName(ctl) = "CheckBox" Then
CkBoxct = CkBoxct + 1
'Debug.Print CkBoxct, ctl.Name
End If
If TypeName(ctl) = "TextBox" Then Txtct = Txtct + 1
If TypeName(ctl) = "OptionButton" Then OptBnct = OptBnct + 1
If TypeName(ctl) = "ComboBox" Then ComBoxct = ComBoxct + 1
Next ctl
MsgBox "labels total " & labelct
MsgBox "Frame total " & Framect
MsgBox "Checkboxes total " & CkBoxct
MsgBox "Textboxs total " & Txtct
MsgBox "OptionButtons total " & OptBnct
MsgBox "ComboBoxs total " & ComBoxct
End Sub
 
Back
Top