+ Reply to Thread
Results 1 to 6 of 6

Trying to toggle Checkbox value

  1. #1
    Registered User
    Join Date
    01-09-2013
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel 2010
    Posts
    3

    Trying to toggle Checkbox value

    Hi
    I have a button which loops through the controls on my form and sets the Checkboxes to true.
    I would like to modify this button so it serves as a toggle feature - ie; set all the Checkboxes = True if they are False, and set all the Checkboxes = False if they are True.

    Private Sub CommandButton1_Click()
    Dim oCtrl As Control
    For Each oCtrl In Me.Controls
    If TypeOf oCtrl Is msforms.CheckBox Then
    oCtrl.Value = True
    End If
    Next
    End Sub

    I tried modifying the above to code to below but it's giving an error, seems like it doesn't like the "oCtrl.Value = False And" part...?

    Private Sub CommandButton1_Click()
    Dim oCtrl As Control
    For Each oCtrl In Me.Controls
    If oCtrl.Value = False And TypeOf oCtrl Is msforms.CheckBox Then
    oCtrl.Value = True
    End If
    If oCtrl.Value = True And TypeOf oCtrl Is msforms.CheckBox Then
    oCtrl.Value = False
    End If
    Next
    End Sub


    Appreciate any input!
    Thanks
    Fergal

  2. #2
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Re: Trying to toggle Checkbox value

    Hi
    Try the following code:

    Please Login or Register  to view this content.
    Seems to work on my test sheet.

    DBY

  3. #3
    Registered User
    Join Date
    01-09-2013
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Trying to toggle Checkbox value

    hi DBY
    thanks for the code but I tried that but it still fails with the same error "Object doesn't support this property or method" error 438

  4. #4
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Re: Trying to toggle Checkbox value

    Sorry I can't reproduce that error, the code works fine on my test sheet. Perhaps another forum member could shed some light on this error. A Google search reveals a number of possible causes. I have created a new workbook and added a User Form with a text box , option box , two check boxes and a command button and run the code from the command button click event and it toggles the check boxes checked/unchecked. Try this to see if it works on a new workbook, it may help to eliminate an application fault or an error within the particular workbook.

    DBY

  5. #5
    Registered User
    Join Date
    01-09-2013
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Trying to toggle Checkbox value

    found a way around, which seems odd but it worked:

    Private Sub CommandButton1_Click()
    'This code selects/de-selects all checkboxes
    Dim oCtrl As Control
    For Each oCtrl In Me.Controls
    If TypeOf oCtrl Is msforms.CheckBox And oCtrl.ForeColor <> RGB(230, 230, 230) Then
    If oCtrl.Value = False Then
    oCtrl.Value = True
    Else: oCtrl.Value = False
    End If
    End If
    Next
    End Sub

  6. #6
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Re: Trying to toggle Checkbox value

    It works also on my test form but the problem is it doesn't just apply to the Check Boxes it also toggles the Option box. But if it does what you need all is well.
    Last edited by DBY; 06-25-2015 at 08:36 AM.

+ 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] Toggle multiple checkboxes (only one checkbox is allowed at a time)
    By Rocky2013 in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 04-14-2015, 06:20 AM
  2. Replies: 1
    Last Post: 02-13-2014, 10:55 PM
  3. Toggle Conditional Format With CheckBox
    By bud11dy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-06-2012, 05:38 AM
  4. Toggle checkbox visibility
    By adam2308 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-18-2010, 07:38 PM
  5. Checkbox toggle true/false
    By Donkin in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-02-2005, 04:37 AM

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