Consulting

Results 1 to 4 of 4

Thread: Easy way to code all toggle buttons?

  1. #1

    Need easy way to code all toggle buttons?

    I have an spread sheet where I created a toggle button linked to cell that when clicked it shows a check mark and turns green when clicked again it removes checkmark and turns red. Is there any code that will combine all the toggle buttons I want to create? What I am looking at now is coping the same code for each toggle button and then changing the values plus the cell. The code listed below is for one toggle button. Probably need over 100 to do the same thing. Thank you in advance!

    Private Sub ToggleButton_Click()
    If ToggleButton.Value = True Then
    ToggleButton.BackColor = vbGreen 'green
    Else
    ToggleButton.BackColor = 255 'red
    End If
    If ToggleButton.Value = True Then
    Range("B2").Interior.Color = vbGreen 'green
    Range("B2").Value = "YES"
    Else
    Range("B2").Interior.Color = 255 'red
    Range("B2").Value = "NO"
    End If
    If ToggleButton.Caption = Chr(254) Then
    ToggleButton.Caption = Chr(168)
    Else
    ToggleButton.Caption = Chr(254)
    End If

    End Sub
    Last edited by classicpcs; 08-22-2014 at 10:31 AM.

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,635
    Private Sub ToggleButton_Click()
        with ToggleButton
             .BackColor = iif(.value,vbGreen,vbred)
             .Caption = Chr(168- 86 * .value)
             Range("B2").Interior.Color = .BackColor
             Range("B2")= format(.value,"YES/No")
       end with
    End Sub
    And have a look over here:

    http://www.snb-vba.eu/VBA_Sheet_Acti...ntrole_en.html
    Last edited by snb; 08-22-2014 at 01:52 PM.

  3. #3
    Thanks, snb but I am getting errors when I try to test. Syntax error. Any suggestions?

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,635
    I amended the previous code, but you learn a lot by debugging the code yourself...

Tags for this Thread

Posting Permissions

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