Visual Basic Private Sub Worksheet_Change Conditional Formats on Multiple Ranges

BexsterBlonde

New Member
Joined
Sep 13, 2010
Messages
4
Hi,

I am trying to set up some Conditional Formats through Code on the Worksheet_Change as I need to setup 4 different Conditions in Excel 2003. Also, the Conditional Formats are different for each Column within the Worksheet. I have managed to setup One Range to change, but I cannot workout how to set up more than One Range. Please can someone help? The below code currently works, but I need to add in different Case Conditional Formats for Columns G2:G250, H2:H250, I2:I250 - how do I do this?

Private Sub Worksheet_Change(ByVal Target As Range)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p></o:p>
Dim Cell As Range<o:p></o:p>
Dim Rng1 As Range<o:p></o:p>
<o:p></o:p>
On Error Resume Next<o:p></o:p>
Set Rng1 = Range("F2:F250").Select<o:p></o:p>
On Error GoTo 0<o:p></o:p>
If Rng1 Is Nothing Then<o:p></o:p>
Set Rng1 = Range(Target.Address)<o:p></o:p>
Else<o:p></o:p>
Set Rng1 = <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:eek:ffice:smarttags" /><st1:place w:st="on">Union</st1:place>(Range(Target.Address), Rng1)<o:p></o:p>
End If<o:p></o:p>
For Each Cell In Rng1<o:p></o:p>
Select Case Cell.Value<o:p></o:p>
<o:p></o:p>
Case 0.00001 To 69.99999<o:p></o:p>
Target.Interior.ColorIndex = 3<o:p></o:p>
<o:p></o:p>
Case 70 To 79.99999<o:p></o:p>
Target.Interior.ColorIndex = 45<o:p></o:p>
<o:p></o:p>
Case 80 To 100<o:p></o:p>
Target.Interior.ColorIndex = 43<o:p></o:p>
<o:p></o:p>
Case 0<o:p></o:p>
Target.Interior.ColorIndex = 56<o:p></o:p>
Target.Font.ColorIndex = 2<o:p></o:p>
<o:p></o:p>
Case Else<o:p></o:p>
Target.Interior.ColorIndex = 15<o:p></o:p>
<o:p></o:p>
End Select<o:p></o:p>
Next<o:p></o:p>
<o:p></o:p>
End Sub

:confused:
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Try something like this. I used the same the conditions for the different columns. You have to change that to your liking.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim Cell As Range
    
    If Not Intersect(Target, Range("F2:F250")) Is Nothing Then
        For Each Cell In Intersect(Target, Range("F2:F250"))
        
            Select Case Cell.Value
            
                Case 0.00001 To 69.99999
                    Cell.Interior.ColorIndex = 3
                
                Case 70 To 79.99999
                    Cell.Interior.ColorIndex = 45
                
                Case 80 To 100
                    Cell.Interior.ColorIndex = 43
                
                Case 0
                    Cell.Interior.ColorIndex = 56
                    Cell.Font.ColorIndex = 2
                
                Case Else
                    Cell.Interior.ColorIndex = 15
            
            End Select
        Next Cell
    End If
    
    If Not Intersect(Target, Range("G2:G250")) Is Nothing Then
        For Each Cell In Intersect(Target, Range("G2:G250"))
        
            Select Case Cell.Value
            
                Case 0.00001 To 69.99999
                    Cell.Interior.ColorIndex = 3
                
                Case 70 To 79.99999
                    Cell.Interior.ColorIndex = 45
                
                Case 80 To 100
                    Cell.Interior.ColorIndex = 43
                
                Case 0
                    Cell.Interior.ColorIndex = 56
                    Cell.Font.ColorIndex = 2
                
                Case Else
                    Cell.Interior.ColorIndex = 15
            
            End Select
        Next Cell
    End If

    If Not Intersect(Target, Range("H2:H250")) Is Nothing Then
        For Each Cell In Intersect(Target, Range("H2:H250"))
        
            Select Case Cell.Value
            
                Case 0.00001 To 69.99999
                    Cell.Interior.ColorIndex = 3
                
                Case 70 To 79.99999
                    Cell.Interior.ColorIndex = 45
                
                Case 80 To 100
                    Cell.Interior.ColorIndex = 43
                
                Case 0
                    Cell.Interior.ColorIndex = 56
                    Cell.Font.ColorIndex = 2
                
                Case Else
                    Cell.Interior.ColorIndex = 15
            
            End Select
        Next Cell
    End If

    If Not Intersect(Target, Range("I2:I250")) Is Nothing Then
        For Each Cell In Intersect(Target, Range("I2:I250"))
        
            Select Case Cell.Value
            
                Case 0.00001 To 69.99999
                    Cell.Interior.ColorIndex = 3
                
                Case 70 To 79.99999
                    Cell.Interior.ColorIndex = 45
                
                Case 80 To 100
                    Cell.Interior.ColorIndex = 43
                
                Case 0
                    Cell.Interior.ColorIndex = 56
                    Cell.Font.ColorIndex = 2
                
                Case Else
                    Cell.Interior.ColorIndex = 15
            
            End Select
        Next Cell
    End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,822
Members
449,096
Latest member
Erald

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top