Conditional Formatting different color same criteria

p9j123

Active Member
Joined
Apr 15, 2014
Messages
288
Office Version
  1. 2013
Platform
  1. Windows
Hi,

I need help on setting a conditional formula. I need the cell to be colored differently once there is already a gap.
Each X represent a column, the first 5 column should be color Red, next 5 X is blue, next 5 X is green. Colors can be anything as long as they are different from each other.

X X X X X A X X X X X B X X X X X
 
Yes Please, that should be fine.

Put the following code in the events of your sheet.
Change the range of A:Z columns by the columns you need

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim r As Range, wr As Range, f As Long, n As Long, ini As Long, fin As Long
    
    Set r = Range([COLOR=#ff0000]"A:Z"[/COLOR])
    If Not Intersect(Target, r) Is Nothing Then
        If Target.Count > 100 Then Exit Sub
        For Each wr In Target.Rows
            f = wr.Row
            n = 1
            ini = 0
            Rows(f).Interior.ColorIndex = xlNone
            For j = 1 To Cells(f, Columns.Count).End(xlToLeft).Column + 1
                If Cells(f, j) = "x" Then
                    If ini = 0 Then ini = j
                Else
                    If ini > 0 Then
                        fin = j - 1
                        Select Case n
                            Case 1
                                Range(Cells(f, ini), Cells(f, fin)).Interior.ColorIndex = 3
                                n = n + 1
                            Case 2
                                Range(Cells(f, ini), Cells(f, fin)).Interior.ColorIndex = 5
                                n = n + 1
                            Case 3
                                Range(Cells(f, ini), Cells(f, fin)).Interior.ColorIndex = 4
                                n = 1
                        End Select
                        ini = 0
                    End If
                End If
            Next
        Next
    End If
End Sub

SHEET EVENT
Right click the tab of the sheet you want this to work, select view code and paste the code into the window that opens up.
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Works like a charm, just one more question how can make the color random so that I am not limited to 3 colors?
 
Upvote 0
Works like a charm, just one more question how can make the color random so that I am not limited to 3 colors?

Try this

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim r As Range, wr As Range, f As Long, n As Long, ini As Long, fin As Long
    
    Set r = Range("A:Z")
    If Not Intersect(Target, r) Is Nothing Then
        If Target.Count > 100 Then Exit Sub
        For Each wr In Target.Rows
            f = wr.Row
            n = 1
            ini = 0
            Rows(f).Interior.ColorIndex = xlNone
            For j = 1 To Cells(f, Columns.Count).End(xlToLeft).Column + 1
                If Cells(f, j) = "x" Then
                    If ini = 0 Then ini = j
                Else
                    If ini > 0 Then
                        fin = j - 1
                        Range(Cells(f, ini), Cells(f, fin)).Interior.ColorIndex = WorksheetFunction.RandBetween(3, 56)
                        ini = 0
                    End If
                End If
            Next
        Next
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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