+ Reply to Thread
Results 1 to 6 of 6

Control if all checkboxes are FALSE (then...)

  1. #1
    Registered User
    Join Date
    04-02-2016
    Location
    Bergen, Norway
    MS-Off Ver
    MS Office 2013
    Posts
    4

    Control if all checkboxes are FALSE (then...)

    Hi, I'm new to this forum and a newbie to coding with vba. I've searched the net for a solution for my current problem, but haven't found any suitable solution yet. Maybe some of you here can help me. Ok, to the topic: I have a set of checkboxes (form control) and I'd like to write a vba code to control if all checkboxes are FALSE (or value = 0). If so, I simply want a MsgBox to appear and then Exit Sub. I've written a code that goes loops through all checkboxes, but it doesn't give the desired end result if all checkboxes are FALSE. Here's the code I have so far:

    For Each cb In ActiveSheet.Shapes
    If cb.Type = msoFormControl Then
    If cb.FormControlType = xlCheckBox Then
    If cb.ControlFormat.Value = 0 Then
    iX = iX + 1
    If iX = 0 Then
    MsgBox "No checkboxes are selected"
    Range("B3").Select
    Exit Sub
    End If
    End If
    End If
    End If
    Next cb

    Any ideas?

    Cheers,
    Magne E.

  2. #2
    Registered User
    Join Date
    08-05-2015
    Location
    Finland
    MS-Off Ver
    Home and Business 2013
    Posts
    50

    Re: Control if all checkboxes are FALSE (then...)

    Hi, you could try to write your third If like this:"if cb.value = false then" (checkbox not checked). Also you add 1 to your iX variable if checkbox is unchecked, what is initial value of it?

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Control if all checkboxes are FALSE (then...)

    For Form controls one tests the checkbox against xlOn (1) or xlOff (-4146).

    change this line
    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  4. #4
    Registered User
    Join Date
    04-02-2016
    Location
    Bergen, Norway
    MS-Off Ver
    MS Office 2013
    Posts
    4

    Re: Control if all checkboxes are FALSE (then...)

    Quote Originally Posted by The_Greg View Post
    Hi, you could try to write your third If like this:"if cb.value = false then" (checkbox not checked). Also you add 1 to your iX variable if checkbox is unchecked, what is initial value of it?
    Hi, and thanks for your quick reply. The initial value of iX is 0. But your suggestion of putting "if cb.value = false..." doesn't seem to work. What happens is that the code loops through all the checkboxes and then simply contiues with the rest of the code (i.e. trying to copy the rows to which the checkboxes are linked). Any ideas?

  5. #5
    Registered User
    Join Date
    04-02-2016
    Location
    Bergen, Norway
    MS-Off Ver
    MS Office 2013
    Posts
    4

    Re: Control if all checkboxes are FALSE (then...)

    Sorry, doesn't seem to work. My code loops through the checkboxes, but doesn't stop if none of them are checked. It simply continues with the rest of the code (i.e. trying to copy the rows to which the checkboxes are linked). Any ideas?

  6. #6
    Registered User
    Join Date
    04-02-2016
    Location
    Bergen, Norway
    MS-Off Ver
    MS Office 2013
    Posts
    4

    Thumbs up Re: Control if all checkboxes are FALSE (then...)

    Hi again, I found a solution! Here's the new code:

    Dim cb As Shape
    Dim valid As Boolean

    valid = False
    For Each cb In ActiveSheet.Shapes
    If cb.Type = msoFormControl Then
    If cb.FormControlType = xlCheckBox Then
    If cb.ControlFormat.Value = xlOn Then
    valid = True
    Exit For
    End If
    End If
    End If
    Next cb

    If valid = False Then
    MsgBox "No checkbox selected"
    Range("B3").Select
    Exit Sub
    End If

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Covert Checkboxes To TRUE/FALSE
    By IcyBricks in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-25-2019, 01:09 PM
  2. clear all checkboxes where values equal FALSE unless a checkbox is chosen
    By analystspec4 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-31-2014, 07:58 PM
  3. How to make the Checkboxes write to records as 0 and 1 and not true false
    By rbpd5015 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-14-2014, 10:35 AM
  4. Macro code to check if all checkboxes on userform are false
    By WilliamWu in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-01-2013, 03:33 PM
  5. Print Sheets based on checkboxes in UserForm, Checkboxes also control another macro
    By krackaberr in forum Excel Programming / VBA / Macros
    Replies: 34
    Last Post: 03-05-2013, 11:12 AM
  6. Creating True or false checkboxes
    By L_ter in forum Access Tables & Databases
    Replies: 1
    Last Post: 03-19-2009, 12:04 AM
  7. [SOLVED] Macro to make all checkboxes false and clear all comboxes
    By ynissel in forum Excel General
    Replies: 5
    Last Post: 07-29-2005, 08:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1