MrExcel Publishing
Your One Stop for Excel Tips & Solutions

Cycling through many checkboxes


Posted by Kale on November 13, 2001 1:20 PM

Is there a way to make this code more condensed?

With UserForm
.CheckBox1.Caption = 1
.CheckBox2.Caption = 2
.CheckBox3.Caption = 3
.CheckBox4.Caption = 4
.CheckBox5.Caption = 5
.CheckBox6.Caption = 6
.CheckBox7.Caption = 7
.CheckBox8.Caption = 8
.CheckBox9.Caption = 9
.CheckBox10.Caption = 10
End with

Thanks for your help


Posted by Juan Pablo on November 13, 2001 1:40 PM

You can use:

For i = 1 to 10
.Controls("CheckBox" & i).Caption = i
Next i

Juan Pablo

Posted by Mark O'Brien on November 13, 2001 1:46 PM

Juan Pablo beat me to it, I was going to suggest his way as an alternative, but I spent 5 minutes getting this sorted so I'll post it anyway:

Dim Ctl As Control
Dim i As Integer


For Each Ctl In Userform.Controls
If Ctl = CheckBox Then
i = i + 1
Ctl.Caption = i
End If
Next

Posted by Kale on November 14, 2001 8:48 AM

Thanks, guys. It did just what I needed.