Solved

VBA clear all check boxes

Posted on 2016-09-18
14
70 Views
Last Modified: 2016-09-18
I would like to reset checkboxes to empty.
The boxes may or may not be checked at the time.

How would I do this?

for instance, uncheck Check Box 14 via VBA?
0
Comment
Question by:Euro5
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 2
14 Comments
 
LVL 47

Expert Comment

by:Martin Liss
ID: 41804022
  Dim cb As Object ' Try Checkbox instead of Object. If Checkbox works change it.
    For Each cb In ActiveSheet.OLEObjects
        If InStr(1, cb.Name, "CheckBox") > 0 Then
            cb.Object.Value = False
        End If
    Next

Open in new window

0
 

Author Comment

by:Euro5
ID: 41804028
Nothing working...this is what I am using - I tried to change Object to Checkbox
Any other ideas?

Sub CLEARALL()
  Dim cb As Object ' Try Checkbox instead of Object. If Checkbox works change it.
    For Each cb In ActiveSheet.OLEObjects
        If InStr(1, cb.Name, "CheckBox") > 0 Then
            cb.Object.Value = False
        End If
    Next

Worksheets("Enter data").Range("A2:F2").ClearContents
Worksheets("Enter data").Range("A4:F4").ClearContents
Worksheets("Enter data").Range("A7:F7").ClearContents
Worksheets("Enter data").Range("A10:F10").ClearContents
Worksheets("Enter data").Range("H4:H10").ClearContents
Worksheets("Enter data").Range("K4:K10").ClearContents
Worksheets("Enter data").Range("M4:M10").ClearContents
Worksheets("Enter data").Range("I15").ClearContents
Worksheets("Enter data").Range("M15").ClearContents

End Sub

Open in new window

0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 41804030
Are your checkboxes ActiveX? If so does the name start with "Checkbox"?
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:Euro5
ID: 41804032
The names are
Check Box 14
Check Box 72
Check Box 47
Check Box 90
Check Box 89
0
 

Author Comment

by:Euro5
ID: 41804033
Ah! They are NOT active X....  just form controls.
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 41804036
Dim cb As Shape
    For Each cb In ActiveSheet.Shapes
       cb.OLEFormat.Object.Value = -4146
    Next

Open in new window

0
 

Author Comment

by:Euro5
ID: 41804038
capture
Does not like it
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 41804039
BTW you can change your range-clearing code to

       Worksheets("Enter data") .Range("A2:F2,A4:F4").ClearContents ' add the rest of the ranges
1
 
LVL 47

Expert Comment

by:Martin Liss
ID: 41804040
It works for me. Are you sure you have form controls checkboxes?
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 41804043
Run the Test macro in the attached.

If you can't figure out what's wrong then please attach a sample workbook.
28970665.xlsm
0
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 500 total points
ID: 41804044
Iterate through the Checkboxes collection and set the value to False...
    Dim cb As CheckBox
    For Each cb In ActiveSheet.CheckBoxes
        cb.Value = False
    Next

Open in new window

0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 41804048
Wayne's code is better then mine because mine will affect all shapes, checkbox or not.
0
 

Author Closing Comment

by:Euro5
ID: 41804081
Yes it worked!! Thanks Wayne!
Thanks for the note on the clear code Martin! ;)
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 41804154
Glad I could help :)

BTW, the below Forms controls can also be handled in the same manner...

    Buttons
    DropDowns
    Spinners
    ListBoxes
    OptionButtons
    GroupBoxes
    Labels
    ScrollBars

...as well as some shapes such as Ovals, Rectangles, Lines, etc.

Wayne
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question