Limit to only 1 selected checkbox
I have imported a table with check-boxes from Access to Excel. Is it possible to set the check-boxes where only one check-box can be selected from that imported table when using Excel?
In the comments Jeeped made an excellent point that radio buttons already have the functionality that you are looking for. On the other hand -- if you prefer the aesthetics of checkboxes then you can certainly use them. I created a userform with two checkboxes in a frame (and no other controls in the frame) and also included a label for displaying the chosen option. The following code deselects all other checkboxes in the frame when one is selected. I used a non-local Boolean variable to circumvent the other checkbox's event handlers while they were being changed to avoid a sort of echo effect I ran into where the events were firing when I didn't want them to (perhaps there is a less kludgy way to do that). The code easily extends to any number of checkboxes in a grouping frame.
Dim selecting As Boolean 'module level variable Private Sub SelectOne(i As Long) Dim c As Control selecting = True For Each c In Frame1.Controls If c.Name <> "CheckBox" & i Then c.Value = False Next c DoEvents Label1.Caption = i & " selected" selecting = False End Sub Private Sub CheckBox1_Click() If Not selecting Then SelectOne 1 End Sub Private Sub CheckBox2_Click() If Not selecting Then SelectOne 2 End Sub
I think this works best and its much easier - at least for a few boxes - for more you could write some formulas in excel and drag down then copy as values and copy paste text from excel into vba. Anyway, here it's how I did it: I went and created code under each button - quite basic
Private Sub DateCheckBox1_Click() If DateCheckBox1.Value = True Then DateCheckBox2.Value = False DateCheckBox3.Value = False End If End Sub Private Sub DateCheckBox2_Click() If DateCheckBox2.Value = True Then DateCheckBox3.Value = False DateCheckBox1.Value = False End If End Sub Private Sub DateCheckBox3_Click() If DateCheckBox3.Value = True Then DateCheckBox2.Value = False DateCheckBox1.Value = False End If End Sub