+ Reply to Thread
Results 1 to 7 of 7

Conditional formatting using VBA

  1. #1
    Forum Contributor
    Join Date
    09-05-2009
    Location
    Cologne, Germany
    MS-Off Ver
    Excel 2010
    Posts
    156

    Question Conditional formatting using VBA

    I must confess that I’m really getting out of my depth here. I am VERY new to VBA and I just have no idea how to solve an issue so any suggestions more than welcome! I have the following syntax;

    Private Sub CheckBox1_Click()
    If CheckBox1 = True Then
    Range("B4:G5") = format this range to fill the cells with light grey
    ElseIf CheckBox1 = False Then
    Range("B4:G4") = do nothing
    End If
    End Sub

    As you can see, I have created a simple check box that when checked should ‘grey out’ or fill in the range of cells light grey. If unchecked, then it should just leave the information in those cells ‘as is’, which is a combination of user input and formula.
    Last edited by kborgers; 03-02-2010 at 06:53 AM. Reason: misleading title

  2. #2
    Forum Contributor
    Join Date
    09-21-2009
    Location
    Belgium - Mechelen
    MS-Off Ver
    Office 365 - version 2310
    Posts
    278

    Smile Re: Conditional formatting using VBA

    Hi,

    I adapted your code with the INTERIOR.COLORINDEX property to set the backgroudcolor of a cell or cells .

    Private Sub CheckBox1_Click()
    Dim Checkbox1
    Checkbox1 = True
    If Checkbox1 = True Then
    Range("B4:G5").Interior.ColorIndex = 15
    '15 will give you the grey color
    Else
    'clear the backgroud color with xlNone
    Range("B4:G5").Interior.ColorIndex = xlNone
    End If
    End Sub

    You can find the colorindex numbers at this website.
    http://www.mvps.org/dmcritchie/excel/colors.htm
    Last edited by MarMo; 03-02-2010 at 08:42 AM.

  3. #3
    Forum Contributor
    Join Date
    09-05-2009
    Location
    Cologne, Germany
    MS-Off Ver
    Excel 2010
    Posts
    156

    Re: Conditional formatting using VBA

    Hey Marmo, many thanks indeed for your suggestion which works brilliantly until I uncheck the box. Having done so, the grey background remains whereas I need it to revert back to the original state. Should I have mentioned this?

  4. #4
    Forum Contributor
    Join Date
    09-21-2009
    Location
    Belgium - Mechelen
    MS-Off Ver
    Office 365 - version 2310
    Posts
    278

    Re: Conditional formatting using VBA

    Quote Originally Posted by kborgers View Post
    Hey Marmo, many thanks indeed for your suggestion which works brilliantly until I uncheck the box. Having done so, the grey background remains whereas I need it to revert back to the original state. Should I have mentioned this?
    Hi ,

    I saw it too late and I 've changed the above code into

    Else
    'clear the backgroud color with xlNone
    Range("B4:G5").Interior.ColorIndex = xlNone

    Hope this helps.

  5. #5
    Forum Contributor
    Join Date
    09-05-2009
    Location
    Cologne, Germany
    MS-Off Ver
    Excel 2010
    Posts
    156

    Re: Conditional formatting using VBA

    Is this not the same as before?

  6. #6
    Forum Contributor
    Join Date
    09-21-2009
    Location
    Belgium - Mechelen
    MS-Off Ver
    Office 365 - version 2310
    Posts
    278

    Re: Conditional formatting using VBA

    Hi kborgers ,

    I attached a simple example with
    1) a checkbox on the worksheet
    2) a checkbox on a userform.

    Both checkboxes work and will change the backgroundcolor when checked or unchecked.
    Check out the VBA code
    If you still got questions , me or others will be here to help.

    Rgds
    MarMo
    Attached Files Attached Files

  7. #7
    Forum Contributor
    Join Date
    09-05-2009
    Location
    Cologne, Germany
    MS-Off Ver
    Excel 2010
    Posts
    156

    Re: Conditional formatting using VBA

    As you can see from the attached, I played around with your suggestion, made some changes and it still works fine. However, when I copy the syntax into my 'real' sheet, it does not work when unchecking the box! Bizarre and ever so frustrating! Many thanks once more for your kind help - I'll keep trying!
    Attached Files Attached Files

+ 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