+ Reply to Thread
Results 1 to 6 of 6

Thread: Conditional Formatting

  1. #1
    Registered User
    Join Date
    11-11-2004
    Posts
    46

    Talking Conditional Formatting

    Hi All,

    I'm designing a holiday chart in work, and am using conditional formatting to colour cells.

    The problem is I have more than 3 conditions. Consequently I need to use a macro.

    I have the following code, but have a couple of questions.

    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim icolor As Integer
    
    
    
        If Not Intersect(Target, Range("G8:GE30", "G35:GH57")) Is Nothing Then
        
    
            Select Case Target
    
                Case 0 To 1
    
                    icolor = 6
    
                
                Case Else
    
                    'Whatever
    
            End Select
    
            
    
            Target.Interior.ColorIndex = icolor
    
        End If
    
    
    
    End Sub
    My range select isn't working as I'd like? I need to select 2 individual ranges at the same time?

    G8:GE30 and G35:GH57 but based on what is entered above excel seems to be selecting G8:GH57? Can anyone tell me how to fix this?

    Also

    Can anyone advise how to include a second condition e.g. if the letter L is inserted in a cell, i'd like to shade it grey?

    I'd appreciate any help!
    Many thanks,
    John

  2. #2
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423
    Hey, you can Union the 2 ranges together like so:

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim icolor As Integer
    If Not Intersect(Target, Union(Range("G8:GE30"), Range("G35:GH57"))) Is Nothing Then
        Select Case Target.Value
            Case 0 To 1
                icolor = 6
            Case Else
                'Whatever
        End Select
        Target.Interior.ColorIndex = icolor
    End If
    End Sub

  3. #3
    Registered User
    Join Date
    11-11-2004
    Posts
    46
    Thanks for that, but can anyone help with conditional formatting for letters also?

    ie If I enter a letter I'd also like to shade the cell a particular colour?

  4. #4
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423
    Simply amend the Select Case as required.

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim icolor As Integer
    If Not Intersect(Target, Union(Range("G8:GE30"), Range("G35:GH57"))) Is Nothing Then
        Select Case Target.Value
            Case 0 To 1
                icolor = 6
            Case "A"
                icolor = 3
            Case Else
                'Whatever
        End Select
        Target.Interior.ColorIndex = icolor
    End If
    End Sub

  5. #5
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,639
    Quote Originally Posted by Jay3 View Post
    Thanks for that, but can anyone help with conditional formatting for letters also?

    ie If I enter a letter I'd also like to shade the cell a particular colour?
    It would have been better to give a better description, ie use of letters in the first post. Change the case select to

     Select Case Target.Value
    Case "H": icolor = 6
    Case "S":icolor=3
    'etc
    Case Else:Exit Sub
    End Select
     Target.Interior.ColorIndex = icolor
    End If
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  6. #6
    Registered User
    Join Date
    11-11-2004
    Posts
    46

    Talking Conditional formatting

    Hi,

    I've got it to work was the suggested range and also the conditional formatting with the lettering now works.

    Strangely I have to put the case for the lettering before the case for the numbering, otherwise it doesn't function.

    Anyway the following code now works, so thanks for your help!

    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim icolor As Integer
    If Not Intersect(Target, Union(Range("G8:GE30"), Range("G35:GH57"))) Is Nothing Then
        Select Case Target.Value
            Case "L"
                icolor = 16
            Case "l"
                icolor = 16
            Case "B"
                icolor = 5
            Case "b"
                icolor = 5
            Case 0 To 1
                icolor = 36
            Case Else
                'Whatever
        End Select
        Target.Interior.ColorIndex = icolor
    End If
    End Sub
    Thanks
    Last edited by Jay3; 11-10-2008 at 04:51 PM. Reason: Tagging

+ 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.2.0