Multiple conditional formatting

JAZ91

New Member
Joined
Sep 28, 2022
Messages
19
Office Version
  1. 365
Platform
  1. Windows
Hi.
I have posted something similar before but have yet to make my code work a 100% yet.
MOST OF THE CODE WORKING!
I put in a lot of code so just want to mention most are working but the addition have not been working as indented.



I have a large worksheet in which there is a few macros running.
One of them is a conditional formatting of some of the columns.

In my column "AD" the cell interior is colored based on whether the status is "open, closed, on hold, N/A, TBD". (This code is working)

In my column "Y" the cell interior is colored based on on due dates and how close they are to the due date (This is working, BUT is missing a feature that I could not get to work. I wanted to add the feature that if the cell did not have any dates in the cell then the cell interior becomes transparent or xlNone.)

My main problem is another formatting is overlapping with the other two. The purpose is to have the status column "AD" have the feature so that if certain statuses such as "Closed" is in the cell, then the cell interior of column "Y" with due dates goes grey. And if the "AD" status is "on hold" then the cell interior and font color of column "Y" with due dates goes blue and red for example.
This formatting should be the one that overrules the other one (middle one).

Currently it is working for a split second but it seems like on formatting overrules the other and changes the color back. So it is like a priority issue i think.

My codes are split between both module and worksheet.
I was thinking it would be nice to have it all in one place like module but I also don't know if that is even possible.

My codes can be seen below, i hope someone can help.







VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)


'Name the range
Dim Color_Range As Range


'Define the range
Set Color_Range = Worksheets("Actionlog").Range("Y4:Y300")

 ' Loop through each cell in the changed range

    For Each cell In Color_Range

        ' Check if the date is overdue (today or earlier)

        If cell.Value <= Date Then

            ' Color the cell red

            cell.Interior.Color = RGB(255, 142, 142) ' Red

        ElseIf cell.Value <= Date + 365 And cell.Value > Date + 7 Then

            ' Color the cell Green

            cell.Interior.Color = RGB(143, 220, 178) ' Green

        ElseIf cell.Value <= Date + 7 Then

            ' Color the cell Orange

            cell.Interior.Color = RGB(242, 204, 89) ' Orange
      

End If

Next cell


End Sub

' The below code is overlapping the on above and gets overwritten when in fact it should be the one set as first priority'

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim CCell As Range
    Dim sht As Worksheet
    Set CCell = Range("AD4:AD300")
    Set sht = Worksheets("Actionlog")
    If Target.Count = 1 Then
        If Not Application.Intersect(CCell, Range(Target.Address)) _
            Is Nothing Then
                If Target.Value = "Closed" Then
                    sht.Cells(Target.Row, 25).Interior.Color = RGB(155, 155, 155)
                End If
        End If
    End If
End Sub


'All the code below is placed in a module opposed to the code above'
'Also below code all working'
'the above code is my new code that im adding to the excel'

Sub Check_status()

Application.ScreenUpdating = False

Dim rg As Range
Dim cond1 As FormatCondition, cond2 As FormatCondition, cond3 As FormatCondition, cond4 As FormatCondition, cond5 As FormatCondition, cond6 As FormatCondition
Set rg = Range("A4:AD300", Range("A4:AD300").End(xlDown))

'clear any existing conditional formatting
rg.FormatConditions.Delete

'define the rule for each conditional format
Set cond1 = rg.FormatConditions.Add(xlCellValue, xlEqual, "Closed")
Set cond2 = rg.FormatConditions.Add(xlCellValue, xlEqual, "Open")
Set cond3 = rg.FormatConditions.Add(xlCellValue, xlEqual, "On hold")
Set cond4 = rg.FormatConditions.Add(xlCellValue, xlEqual, "Done")
Set cond5 = rg.FormatConditions.Add(xlCellValue, xlEqual, "TBD")
Set cond6 = rg.FormatConditions.Add(xlCellValue, xlEqual, "N/A")

'define the format applied for each conditional format
'Green - RBG(143,220,178), Purple - RGB (178, 162, 197), Grey - RGB(155, 155, 155), Red - RGB(255, 142, 142), Orange - RGB(242, 204, 89), Blue - RGB(87, 193, 231)
With cond1
.Interior.Color = RGB(143, 220, 178)
.Font.Color = vbBlack
End With

With cond2
.Interior.Color = RGB(255, 142, 142)
.Font.Color = vbWhite
End With

With cond3
.Interior.Color = RGB(242, 204, 89)
.Font.Color = vbRed
End With

With cond4
.Interior.Color = RGB(87, 193, 231)
.Font.Color = vbWhite
End With

With cond5
.Interior.Color = RGB(178, 162, 197)
.Font.Color = vbBlack
End With

With cond6
.Interior.Color = RGB(155, 155, 155)
.Font.Color = vbBlack
End With

Application.ScreenUpdating = True

End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
When you hit the ENTER key to make an edit change, this will successively fire Worksheet_Change and Selection_Change.

Your code for both events is changing the colour in column Y, so presumably this explains the problem you're experiencing.

It's not clear why you're using a mix of VBA and conditional formatting, or why you're looping through Y4:Y300 any time there is a selection change anywhere in the worksheet.

Can't you simply conditionally format column Y based on a couple of rules? If you could explain those rules, I'm sure we could help further.
 
Upvote 0
When you hit the ENTER key to make an edit change, this will successively fire Worksheet_Change and Selection_Change.

Your code for both events is changing the colour in column Y, so presumably this explains the problem you're experiencing.

It's not clear why you're using a mix of VBA and conditional formatting, or why you're looping through Y4:Y300 any time there is a selection change anywhere in the worksheet.

Can't you simply conditionally format column Y based on a couple of rules? If you could explain those rules, I'm sure we could help further.
Hi StephenCrump

First of all thank you for your response.

I am not very proficient in coding in VBA so I might say some nonsense, but I thought the above code I am showing was all considered VBA and not conditional formatting.

The code is taken from multiple other peoples code and I have tried to cut and combine into what I needed.


So I will try to state the rules I need.
Keep in mind that column "Y4:Y300" is due dates and column "AD4:AD300" contain statuses such as "open, closed, on hold, etc."

1. If current date is 14 days from the due date --> then color interior green
2. If current date is 7 days from the due date --> then color interior yellow
3. If current date is 3 days from the due date --> then color interior orange
4. If current date is on the due date or over/later --> then color interior red
5. If no due dates is in the cell --> then color interior transparent

6. If the status in column AD is "Closed" --> then color interior of due date grey (This rule needs to overrule the rules 1-5)


Lastly, I have made a conditional formatting on the AD column with statuses that color the interior depending on the text in it. However I do not think that interferes with what I am trying to achieve above.

I wanted to collect the code in one place for my formatting but I do not know if that is doable / easier, I think it just my OCD that like things orderly like that :LOL:


Anyway I hope what I said makes sense and let me know if I need to explain more :)

BR/

JAZ
 
Upvote 0
I tend to agree with @StephenCrump re using CF for your issue. Would the following not work for you?

CF.xlsx
YZAAABACAD
3Hdr1Hdr2Hdr3Hdr4Hdr5Hdr6
426/11/2024open
518/11/2024closed
614/11/2024on hold
711/11/2024TBD
8closed
926/11/2024closed
1018/11/2024open
1114/11/2024closed
1212/11/2024N/A
1330/11/2024N/A
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
Y4:Y13Expression=AND($Y4<>"",$AD4<>"closed",$Y4-TODAY()<=0)textYES
Y4:Y13Expression=AND($Y4<>"",$AD4<>"closed",$Y4-TODAY()<=3)textYES
Y4:Y13Expression=AND($Y4<>"",$AD4<>"closed",$Y4-TODAY()<=7)textYES
Y4:Y13Expression=AND($Y4<>"",$AD4<>"closed",$Y4-TODAY()>=14)textYES
Y4:Y13Expression=AND($Y4<>"",$AD4="closed")textYES
 
Upvote 0
I thought the above code I am showing was all considered VBA ....
Yes, you've used VBA. But the VBA uses a mix of conditional formatting and changing the cells' .Interior.Color directly.

The problem is due to your use of the Worksheet_Change and Selection_Change events. Not necessary if you're happy with conditional formatting.

@kevin9999 just beat me to it. Here's my (very similar) take. (And you could use VBA to apply this if you wanted to).

YZAAABACAD
1CurrentDate12 Nov 2024
2
3Due DateStatus
410 Nov 2024CLOSED
511 Nov 2024
612 Nov 2024
713 Nov 2024
815 Nov 2024
916 Nov 2024
1019 Nov 2024
1119 Nov 2024CLOSED
1220 Nov 2024
1326 Nov 2024CLOSED
1426 Nov 2024
1527 Nov 2024
1628 Nov 2024CLOSED
Sheet1
Cell Formulas
RangeFormula
Z1Z1=TODAY()
Cells with Conditional Formatting
CellConditionCell FormatStop If True
Y4:Y16Expression=AD4="Closed"textNO
Y4:Y16Expression=NOT(ISNUMBER(Y4))textNO
Y4:Y16Expression=Y4<=CurrentDatetextNO
Y4:Y16Expression=Y4-3<=CurrentDatetextNO
Y4:Y16Expression=Y4-7<=CurrentDatetextNO
Y4:Y16Expression=Y4-14<=CurrentDatetextNO
 
Upvote 0
Hi @kevin9999 and @StephenCrump

Thank you both for the answers and time spent on them.

This is exactly the outcome I am looking for.
However, I am going to be really annoying and ask what the solution would be if I want to have the code in the VBA ?
Is it not possible to have the same rules/codes written in the visual basic window?

Best regards

JAZ :)
 
Upvote 0
Is it not possible to have the same rules/codes written in the visual basic window?
Can you please clarify?

Do you mean VBA applying conditional formatting rules similar to Posts #4 and #5? That would be similar to your Sub Check_status from Post #1. You could use the macro recorder to get you started with the syntax, and tidy up as required.

Or do you want VBA to loop through the cells changing .Interior.Color? (Which would have the disadvantage of not being dynamic, like conditional formatting).

Or are you envisaging some sort of event driven code, e.g. perhaps based on Worksheet_Change?

(You originally had a mix of different approaches, and it wasn't clear what you were trying to achieve).
 
Upvote 0

Forum statistics

Threads
1,223,929
Messages
6,175,461
Members
452,645
Latest member
Tante

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