VBA Macro help with checkboxes involved

usnapoleon

Board Regular
Joined
May 22, 2014
Messages
81
Office Version
  1. 365
Platform
  1. Windows
Hello
I have an existing Macro that I got from a cool person on here where, upon checking 1 box it would uncheck any others, thus preventing errors. I need some help again, and the difference now is that I have 2 sets of checkboxes that are independent of each other. However, when I check 1 on one side, it unchecks the box on the other side. I'll show you:

Voucher Spreadsheet.xlsm
ABCDEFGHIJKLMN
16
17Voucher Itemization
18DepartmentVoucher Type
19CGMPPHSHTHRWFVSBTHNot Reduced
20Cashier NameCk #Canyon GrilleMarket PlacePizza HutThe ShopTheaterDeptResort WideFood VoucherStarbucksTheaterTypeAMOUNT
21SH 5.00
1
Cell Formulas
RangeFormula
H21H21=IFERROR(INDEX($C$19:$G$19,MATCH(TRUE,C21:G21,0)),"")
M21M21=IFERROR(INDEX($I$19:$L$19,MATCH(TRUE,I21:L21,0)),"")


columns C-G are the original checkboxes, and the VBA Macro was built for them originally. I added the 2nd set of I-L to the spreadsheet. C-G are independent of columns I-L. There has to be 1 box checked in each group but it's not letting me.

Here is the VBA macro coding that is assigned:
VBA Code:
Option Explicit
Sub Change_Check_Boxes()
    Dim c As Excel.CheckBox, cx As Excel.CheckBox
    Set c = ActiveSheet.CheckBoxes(Application.Caller)
    For Each cx In ActiveSheet.CheckBoxes
        If cx.TopLeftCell.Column <> c.TopLeftCell.Column _
        And cx.TopLeftCell.Row = c.TopLeftCell.Row Then cx.Value = xlOff
    Next cx
End Sub

I know nothing of VBA so I couldnt even attempt to review and try to modify this. I have a feeling I need a 2nd macro for the new checkboxes in columns I-L. I'm just guessing though.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
i can't see the checkboxes in your example, but I am not sure how this code does the job for you.
The code you posted will only unselect checkboxes if they are well scattered on the sheet - boxes matching the row or column of the clicked checkbox will not be unselected.
Using the index property is much reliable in this context.
for your particular request try this modification:
VBA Code:
Sub Change_Check_Boxes()
    Dim c As Excel.CheckBox, cx As Excel.CheckBox
    Set c = ActiveSheet.CheckBoxes(Application.caller)
    For Each cx In ActiveSheet.CheckBoxes
        If (cx.TopLeftCell.Column > 8) = (c.TopLeftCell.Column > 8) Then
            If cx.Index <> c.Index Then cx.Value = xlOff
        End If
    Next cx
End Sub
 
Upvote 0
i can't see the checkboxes in your example, but I am not sure how this code does the job for you.
The code you posted will only unselect checkboxes if they are well scattered on the sheet - boxes matching the row or column of the clicked checkbox will not be unselected.
Using the index property is much reliable in this context.
for your particular request try this modification:
VBA Code:
Sub Change_Check_Boxes()
    Dim c As Excel.CheckBox, cx As Excel.CheckBox
    Set c = ActiveSheet.CheckBoxes(Application.caller)
    For Each cx In ActiveSheet.CheckBoxes
        If (cx.TopLeftCell.Column > 8) = (c.TopLeftCell.Column > 8) Then
            If cx.Index <> c.Index Then cx.Value = xlOff
        End If
    Next cx
End Sub
Hi Bobsan42 - Sorry I did a preview of the message and it didnt dawn on me that the actual checkboxes dont show! doh!

1712259181635.png


Above is how the current sheet looks. The row and column numbers are as from my opening post, it just doesn't do justice to the visual aspect of it.

The code I shared would affect only the same row I was working on. If i checked Canyon Grille's box, and then check The Shop, the Canyon Grille would uncheck. It worked just fine in that regard. There were just the 5 columns of the 'Departments'.
Then I decided to add the 2nd group of checkboxes, the 'Voucher Type'. This is where it started messing up: if I check Canyon Grille, and then Starbucks, the Canyon Grille unchecks - I dont want that. The 'Department' section can only have 1 checked box per row, so if Canyon Grille gets checked then any other checked department should uncheck on that row. The 'Voucher Type' section functions the same way, so if Resort Wide is checked then any other Voucher type should uncheck on that row. A Department checked-box should not affect a Voucher checked-box, and vice versa.

Reviewing the VBA above, will this work now that I've given more details? Will the macro be assigned to all check boxes? Thank you very much for helping me with this!!
 
Upvote 0
Did you try the code i posted?
 
Upvote 0
It does work! Yay! Sorry for not testing it first, I just wasnt sure mainly if it was a 2nd macro or a total replacement. I dont know enough about VBA to simply look at it and know. Thank you very very much!!!! @bobsan42
 
Upvote 0
Did you try the code i posted?
Hi again
Actually, it is having a problem. it is properly unchecking the row and group it's related to, but it's also unchecking the column, so it's affecting other rows. Do you know why?
 
Upvote 0
yes, now that i see what you mean,i think.
try like this:
VBA Code:
Sub Change_Check_Boxes()
    Dim cb As Excel.CheckBox, cbx As Excel.CheckBox
    Dim c As Range, cx As Range
    Dim i As Long
 
    Set cb = ActiveSheet.CheckBoxes(Application.Caller)
    i = cb.Index
    Set c = cb.TopLeftCell
 
    For Each cbx In ActiveSheet.CheckBoxes
        If i <> cbx.Index and cbx.Value = xlOn Then
            Set cx = cbx.TopLeftCell
            If c.Row = cx.Row Then
                If (c.Column > 8) = (cx.Column > 8) Then
                    cbx.Value = xlOff
                End If
            End If
        End If
    Next cbx
End Sub
if each row is a separate group.
 
Last edited:
Upvote 1
Solution
yes, now that i see what you mean,i think.
try like this:
VBA Code:
Sub Change_Check_Boxes()
    Dim cb As Excel.CheckBox, cbx As Excel.CheckBox
    Dim c As Range, cx As Range
    Dim i As Long
 
    Set cb = ActiveSheet.CheckBoxes(Application.Caller)
    i = cb.Index
    Set c = cb.TopLeftCell
 
    For Each cbx In ActiveSheet.CheckBoxes
        If i <> cbx.Index and cbx.Value = xlOn Then
            Set cx = cbx.TopLeftCell
            If c.Row = cx.Row Then
                If (c.Column > 8) = (cx.Column > 8) Then
                    cbx.Value = xlOff
                End If
            End If
        End If
    Next cbx
End Sub
if each row is a separate group.
Sorry, this was not working either.

When I check one box, then another (in the same group), it does not uncheck the first box. So I'm able to have multiple boxes at the same time.
1712287317419.png
 
Upvote 0
Please try the following on a copy of your workbook.

VBA Code:
Option Explicit
Sub Change_Check_Boxes()
    Dim c As Excel.CheckBox, cx As Excel.CheckBox
    Set c = ActiveSheet.CheckBoxes(Application.Caller)
    For Each cx In ActiveSheet.CheckBoxes
        If c.TopLeftCell.Column >= 3 And c.TopLeftCell.Column <= 7 Then
            If cx.TopLeftCell.Column >= 3 And cx.TopLeftCell.Column <= 7 Then
                If cx.TopLeftCell.Column <> c.TopLeftCell.Column _
                And cx.TopLeftCell.Row = c.TopLeftCell.Row _
                Then cx.Value = xlOff
            End If
        End If
        If c.TopLeftCell.Column >= 9 And c.TopLeftCell.Column <= 13 Then
            If cx.TopLeftCell.Column >= 9 And cx.TopLeftCell.Column <= 13 Then
                If cx.TopLeftCell.Column <> c.TopLeftCell.Column _
                And cx.TopLeftCell.Row = c.TopLeftCell.Row _
                Then cx.Value = xlOff
            End If
        End If
    Next cx
End Sub
 
Upvote 1
what do you consider a group of checkboxes?
the code works, maybe what it does is not what you expect.
All checkboxes on one row to the left or right of column H are considered a group. The code will allow you to have 1 or 0 checked boxes in a group.
just tested kevin9999's code it also works in (almost) the same way. so you have to make one of these two work for you.
 
Upvote 0

Forum statistics

Threads
1,215,214
Messages
6,123,661
Members
449,114
Latest member
aides

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