Coniditional Formatting Applying Randomly or to Whole Sheet

AlexB123

Board Regular
Joined
Dec 19, 2014
Messages
207
Hi everyone,

I need a macro to change the font of entire rows of data for two conditions.

(1) If column C (age of column B) is greater than 30 days, but less than 61 days, the font should be BOLD and BLACK.
(2) If Column C is greater than 60 days, the font should be BOLD AND RED.

I'm definitely getting frustrated ... this should be so simple.

Code:
   Dim wrkRng As Range
 
  With ActiveWorkbook.Worksheets("Sheet1").Range("A1:M1071")
            .FormatConditions.Delete
            Set wrkRng = ActiveWorkbook.Worksheets("Sheet1").Range("A1:K1071")
        End With
       
   With wrkRng
            .FormatConditions.Add Type:=xlExpression, Formula1:="=AND($C2>30,$C2<=60)"
    With .FormatConditions(1).Font
     .Bold = True
     .Color = RGB(0, 0, 0)
    End With
        End With
    
        With wrkRng
            
            .FormatConditions.Add Type:=xlExpression, Formula1:="=$C2>=61"
            With .FormatConditions(2).Font
                .Bold = True
                .Color = RGB(255, 0, 0)
            End With
        End With
 
Yes, the formula is correct, but does not work when it is produced via VBA.

When I delete all rules, and input the same formula manually, it works as expected.
 
Upvote 0

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
When the rules are created by VBA, it does the following:

a) it highlights the row above the one that should be highlighted, when the row that should be highlighted is not followed by another row that should be highlighted

b) if there are two successive rows that should be highlighted, the row before the first and the row above the second row are highlighted.

i.e.,

7 (bold)
34 (no formatting)
6

7 (bold and red)
62(bold and black)
34 (no formatting)
 
Upvote 0
UPDATE!!!

After the nonsense above, I applied the code I created to a broader macro that opens a workbook and creates a file from that one. IT SEEMS TO WORK!!!

LOL. I have no idea why my test workbook and macro applies the conditional formatting in such an odd way ... I would be very interested to hear any suggestions as to why it applied the rules as it did. I will keep my fingers crossed this new macro continues to operate as expected next week.

Thanks!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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