Help with Selecting multiple Checkboxes on a form

C

Corey ....

I am trying to use a CommnadButton on a form to Select ALL Checkboxes on a
form IF the CheckBox Caption is NOT "----------".
However i keep getting a error on the below identified line, why ?

Private Sub CommandButton2_Click()
Dim chx As CheckBox
With UserForm6
If chx.Caption <> "----------" Then '<==== ERROR
For Each chx In UserForm6
chx.box = True
Next chx
End If
End With
End Sub

How can i fix this?

Corey....
 
O

OssieMac

Hi Corey,

If you want to use the variable instead of the checkbox name you need to
assign a checkbox to it first otherwise how does it know which checkbox
caption you are referring to. Because you are using chx in the For Each loop,
perhaps simply using the actual name of the checkbox to identify the caption.
 
J

JLGWhiz

Adding to Ossie's observation, moving your If statement inside the For ...
Next loop will accomplish the variable assignment through the For Each chx
part of the statement. Then you will need to fix the statembent:

chx.box = True

Looks like it should be just:

chx = True
 
J

JLGWhiz

You also need to modify this line:

For Each chx In UserForm6

To:

For Each chx In UserForm6.Controls

This assumes you only have checkboxes on the form.
 
D

Dave Peterson

Option Explicit
Private Sub CommandButton2_Click()
Dim ctrl As control 'msforms.CheckBox
for each ctrl in me.controls
if typeof ctrl is msforms.checkbox then
if ctrl.caption = "----------" then
'do nothing
else
ctrl.value = true
end if
end if
next ctrl
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top