How do I create a reset button that unchecks checkboxes in my spreadsheet - but not all of them?

Alphaba1

New Member
Joined
Oct 23, 2024
Messages
3
Office Version
  1. Prefer Not To Say
Platform
  1. Windows
I have a spreadsheet with numerous checkboxes in Row A. I have created a reset button to uncheck the buttons, however I can only find a code for macros that will reset ALL the checkboxes. I do not want the checkbox in cell A26 to reset when the button is pressed. Is there a way to exclude specific checkboxes? I am a complete newbie, so apologies if the answer is an obvious one. Thankyou.
Below is the code I have:

VBA Code:
Sub ResetDataClearCB()
For Each cb In Worksheets("COMMERCIAL").checkboxes
cb.Value = x1Off
Next
End Sub
 

Attachments

  • Screenshot checkboxes.png
    Screenshot checkboxes.png
    22.4 KB · Views: 5
Last edited by a moderator:

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Welcome to the MrExcel board!

When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊

I also suggest that you set 'Require Variable Declaration' in the VBA window: Tools -> Options -> Editor
I suspect that in your code you did not mean to have the digit "1" in x1Off but rather meant a lower case "L" xlOff
Having the setting I mentioned would catch things like that.


checkboxes in Row A.
Further, it will help avoid confusion if you use the correct terminology. Where you have your checkboxes is Column A. :)


Assuming they are Form Controls checkboxes, try like this

VBA Code:
Sub ResetDataClearCB_v2()
  Dim cb As CheckBox
 
  For Each cb In Worksheets("COMMERCIAL").CheckBoxes
    If cb.Caption <> "Fuel Surcharge" Then cb.Value = xlOff
  Next
End Sub
 
Upvote 0
Hi welcome to formum

If it is just the one checkbox you want the code to ignore then the addition of a simple If statement should do what you want

VBA Code:
Sub ResetDataClearCB()
    Dim cb As CheckBox
    For Each cb In Worksheets("COMMERCIAL").CheckBoxes
      If cb.TopLeftCell.Address(0, 0) <> "A26" Then cb.Value = x1Off
    Next
End Sub

However, if you need to include multiple checkboxes then you may find Select Case statement a better way to go

VBA Code:
Sub ResetDataClearCB()
    Dim cb As CheckBox
    For Each cb In Worksheets("COMMERCIAL").CheckBoxes
     Select Case cb.TopLeftCell.Address(0, 0)
     Case "A26", "A17", "A10"
      'do nothing
     Case Else
        cb.Value = x1Off
    End Select
    Next
End Sub

Dave
 
Upvote 0
Welcome to the MrExcel board!

When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊

I also suggest that you set 'Require Variable Declaration' in the VBA window: Tools -> Options -> Editor
I suspect that in your code you did not mean to have the digit "1" in x1Off but rather meant a lower case "L" xlOff
Having the setting I mentioned would catch things like that.



Further, it will help avoid confusion if you use the correct terminology. Where you have your checkboxes is Column A. :)


Assuming they are Form Controls checkboxes, try like this

VBA Code:
Sub ResetDataClearCB_v2()
  Dim cb As CheckBox
 
  For Each cb In Worksheets("COMMERCIAL").CheckBoxes
    If cb.Caption <> "Fuel Surcharge" Then cb.Value = xlOff
  Next
End Sub
Thankyou so much for your help and advice. I do apologize for my mistakes. I'm learning a lot. That looks like its done the trick :)
 
Upvote 0
Hi welcome to formum

If it is just the one checkbox you want the code to ignore then the addition of a simple If statement should do what you want

VBA Code:
Sub ResetDataClearCB()
    Dim cb As CheckBox
    For Each cb In Worksheets("COMMERCIAL").CheckBoxes
      If cb.TopLeftCell.Address(0, 0) <> "A26" Then cb.Value = x1Off
    Next
End Sub

However, if you need to include multiple checkboxes then you may find Select Case statement a better way to go

VBA Code:
Sub ResetDataClearCB()
    Dim cb As CheckBox
    For Each cb In Worksheets("COMMERCIAL").CheckBoxes
     Select Case cb.TopLeftCell.Address(0, 0)
     Case "A26", "A17", "A10"
      'do nothing
     Case Else
        cb.Value = x1Off
    End Select
    Next
End Sub

Dave
Hi Dave, that's perfect! Thanks so much. I do actually need to include multiple checkboxes, so this is great. Thanks again for your help.
 
Upvote 0
I do actually need to include multiple checkboxes
A similar approach can be taken with the CheckBox captions so it is probably a matter of deciding what is most reliable for your circumstances.
Using .TopLeftCell.Address could possibly be an problem if rows are added or deleted.
Using the CheckBox caption could be a problem if the captions might be changed.

Anyway, if going with Captions, it would be something like this
VBA Code:
Sub ResetDataClearCB_v3()
  Dim cb As CheckBox
  
  For Each cb In Worksheets("COMMERCIAL").CheckBoxes
    Select Case cb.Caption
      Case "Fuel Surcharge", "WA Regional Surcharge"  '<- Add more if required
      Case Else:  cb.Value = xlOff
    End Select
  Next cb
End Sub
 
Upvote 0
Hi Dave, that's perfect! Thanks so much. I do actually need to include multiple checkboxes, so this is great. Thanks again for your help.
You are welcome & glad we were able to help & appreciate your feedback.
Please remember to mark the solution you used.

Dave
 
Upvote 0

Forum statistics

Threads
1,223,102
Messages
6,170,122
Members
452,303
Latest member
c4cstore

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