Macro Conditional Formatting

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,601
Office Version
  1. 2021
Platform
  1. Windows
I have the following code to format data using CF. Where the value in Col J from row 12 onwards is >=30, then I need , to highlight from column A to J and 2 rows below the value conating a value in Col J that is 30 and greater

For Eg if J12 is say 35 then A12 to A14 must be highlighted in Gray and so on i.e for each value that is >=30 Col A to J + 2 rows below the column containing a value >= 30 must be highlighted using CF

For some unkown reason, this is not happening

Sub conditional_formating()
Sheets("Sheet1").Select
Finalrow = Range("a12").End(xlDown).Row
With Range("A12:J" & Finalrow)
.Select
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:="=$J12>=30"
.FormatConditions(1).Interior.PatternColorIndex = xlAutomatic
.Resize(2).FormatConditions(1).Interior.ColorIndex = 15
End With
End Sub

It would be appreciated if someone could assist-see sample data after running Macro and what sample data should look like

Sample Data After running conditional Formatting

Excel Workbook
ABCDEFGHIJ
1*12/09/2011********
212/09/2011 15:06*********
3**********
4**********
5*******YW*
6**********
7**********
8REFTypeDateStoZk No.AmountVehicle/CustomerPartsLabourOtherAge
9**********
10**********
11**********
12ZZ3815ZEB01/09/2011ZBK448EK000**35
13***1.4AMBIENTE0****
14**********
15**********
16ZZ3819ZEB01/09/2011ZHJ290EK000**12
17***2.5TZ4X2D/C*0**
18**********
19**********
20ZZ3835ZEB02/09/2011ZXW724EK000**35
21***130STINGSEDAN*0**
22**********
23**********
24ZZ3838ZEB02/09/2011ZLR803EK000**41
25***00*****
26**********
27**********
28ZZ3847ZEB05/09/2011ZXZ155EK000**48
29***00*****
30*ZEL********
31**********
32ZZ3848ZEB05/09/2011ZLR109EK000**18
33***00*****
34*ZEL********
35**********
36ZZ0779ZEL14/12/2010BRL271B29365.1020**153
37***3XLTSUPER029385.1**
38*ZEL*GARAGEBH*****
39**********
40ZZ2464ZEL11/05/2011ZSZ391EK00300**105
41***ZOZUS1.8TDCIT0300**
42*ZEL*KITZHING******
Sheet1


Sample Data-Manually coloring data to show wehat it should lok like if running conditional formatting

Excel Workbook
ABCDEFGHIJ
1*12/09/2011********
212/09/2011 15:06*********
3**********
4**********
5*******YW*
6**********
7**********
8REFTypeDateStoZk No.AmountVehicle/CustomerPartsLabourOtherAge
9**********
10**********
11**********
12ZZ3815ZEB01/09/2011ZBK448EK000**35
13***1.4AMBIENTE0****
14**********
15**********
16ZZ3819ZEB01/09/2011ZHJ290EK000**12
17***2.5TZ4X2D/C*0**
18**********
19**********
20ZZ3835ZEB02/09/2011ZXW724EK000**35
21***130STINGSEDAN*0**
22**********
23**********
24ZZ3838ZEB02/09/2011ZLR803EK000**41
25***00*****
26**********
27**********
28ZZ3847ZEB05/09/2011ZXZ155EK000**48
29***00*****
30*ZEL********
31**********
32ZZ3848ZEB05/09/2011ZLR109EK000**18
33***00*****
34*ZEL********
35**********
36ZZ0779ZEL14/12/2010BRL271B29365.1020**153
37***3XLTSUPER029385.1**
38*ZEL*GARAGEBH*****
39**********
40ZZ2464ZEL11/05/2011ZSZ391EK00300**105
41***ZOZUS1.8TDCIT0300**
42*ZEL*KITZHING******
Sheet1
 
Last edited:

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hello Howard,

Your errors are your $J12 should be $J$12 and you need to offset the end down by -1

If you want it to look like what you did manually change the -1 to -2 After Row in the code.

Try this code revision ~

Code:
Sub conditional_formating()
Sheets("Sheet1").Select
Finalrow = Range("a12").End(xlDown).Row - 1

With Range("A12:J" & Finalrow)
.Select
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:="=$J$12>=30"
.FormatConditions(1).Interior.PatternColorIndex = xlAutomatic
.Resize(2).FormatConditions(1).Interior.ColorIndex = 15
End With
End Sub

Cheers
 
Upvote 0
Hello again,

Just thought you probably wish to do the whole sheet format thing in one hit.

If you do, try using a helper column that is unused. For my example in the code I am using column AC

In Cell AC12 Copy and paste this formula
=IF(ISNUMBER(J12),J12,IF(ISNUMBER(J13),1,AC11))
Now fill that down for the rows that you have data in.

If you have more than 1000 rows of data change this bit of code to suit
With Range("A12:J1000")

Then try this code ~

Code:
Sub conditional_formating()
Sheets("Sheet1").Select
With Range("A12:J1000")
.Select
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:="=$AC12>=30"
.FormatConditions(1).Interior.PatternColorIndex = xlAutomatic
.Resize(2).FormatConditions(1).Interior.ColorIndex = 15
End With
End Sub

Hope that works for you.
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

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