+ Reply to Thread
Results 1 to 2 of 2

Conditional formatting using a macro.

Hybrid View

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

    Talking Conditional formatting using a macro.

    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 Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387
    Jay3,

    This works for me:

    
    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Intersect(Target, Range("G8:GE30, G35:GH57")) Is Nothing Then Exit Sub
        If Target.Count > 1 Then Exit Sub
        Dim icolor As Integer
        Application.EnableEvents = False
        Application.ScreenUpdating = False
        Select Case Target.Value
            Case 0 To 1
                Target.Interior.ColorIndex = 6
            Case Else
                'Whatever
        End Select
        Application.EnableEvents = True
        Application.ScreenUpdating = True
    End Sub

    Have a great day,
    Stan

+ 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