+ Reply to Thread
Results 1 to 3 of 3

Toggle Code to format-unformat range of cells

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-01-2008
    Location
    Perth, Australia
    MS-Off Ver
    Excel 365
    Posts
    135

    Red face Toggle Code to format-unformat range of cells

    Hi All,

    I am trying to combine the 2 macro strings below.
    This is to change the font colour in a range of cells so as to hide the inputs and then unhide them when toggle button is unclicked.

    Any recommendations on a shorter line of code would be much appreciated.

    Thanks

    
    Private Sub ToggleButton2_Click()
    If ToggleButton2.Value = True Then
    
        Range("A9:A" & Cells(Rows.Count, 1).End(xlUp).Row).Select
        With Selection.Font
            .FontStyle = "Regular"
            .Size = 10
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .ColorIndex = xlNone
          ' or .ColorIndex = 2
            
        End With
    End Sub
    
    
    
    
    Private Sub ToggleButton2_Click()
    If ToggleButton2.Value = False Then
    
        Range("A9:A" & Cells(Rows.Count, 1).End(xlUp).Row).Select
        With Selection.Font
            .FontStyle = "Regular"
            .Size = 10
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .ColorIndex = xlAutomatic
            
        End With
    End Sub

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    This should do it
    Private Sub ToggleButton2_Click()
    Range("A9:A" & Cells(Rows.Count, 1).End(xlUp).Row).Font.ColorIndex = IIf(ToggleButton2.Value, 2, xlAutomatic)
    End Sub
    I was getting errors trying to set the .ColorIndex to xlNone, so I used 2 (white).

    If there were more properties to be toggled, an If Else EndIf would be a good way to go.
    Last edited by mikerickson; 02-09-2008 at 01:39 AM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Forum Contributor
    Join Date
    02-01-2008
    Location
    Perth, Australia
    MS-Off Ver
    Excel 365
    Posts
    135

    Thumbs up Awesome

    Hi Mike,

    That works a treat.

    Thanks so much for your help.

    Cheers

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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