Limit number of checkboxes-Help please

Tjtrev89

Board Regular
Joined
Sep 13, 2013
Messages
56
Hi all

Currently developing a test type document, which incorporates option buttons as well as check boxes.

In one question, I want the user to select 6 answers or checkboxes out of a possible 8. Is there something that I can do to limit the number of checkboxes that they can select? I have previously used vb but have not done so in a while, so I'll provide detail below if this is the route I need to take.

They are named CheckBox 1 to CheckBox 8 respectively
They have groupnames Question 5.1 to 5.8 respectively (ie all different groups)
They are within a worksheet called Module 1

Really appreciate any help!

Thanks
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I should now add that I'm now trying this vb code but is not working, says 'Complie Error, Expected End of Statement'.

Can anyone help fix this please? Many Thanks


Private Const MAX_CHECKED_BOX_COUNT As Integer = 6 Private Sub CheckBoxes_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox6.CheckedChanged, CheckBox5.CheckedChanged, CheckBox4.CheckedChanged, CheckBox3.CheckedChanged, CheckBox2.CheckedChanged, CheckBox1.CheckedChanged 'Get all the CheckBoxes. Dim checkBoxes = Controls.OfType(Of CheckBox)().ToArray() 'Get the number of checked CheckBoxes. Dim checkedBoxCount = checkBoxes.Count(Function(cb) cb.Checked) 'Unchecked CheckBoxes should be enabled if and only if the number of checked CheckBoxes is less than the maximum number allowed. Dim enableUncheckedBoxes = checkedBoxCount < MAX_CHECKED_BOX_COUNT 'Get the unchecked CheckBoxes. Dim uncheckedBoxes = checkBoxes.Where(Function(cb) Not cb.Checked) 'Enable or disable the unchecked CheckBoxes as appropriate. For Each uncheckedBox In uncheckedBoxes uncheckedBox.Enabled = enableUncheckedBoxes Next End Sub
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,195
Members
453,021
Latest member
pingpong7117

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top