Any one know why this macro is not working?

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone, this macro works great but as soon as i added

VBA Code:
Sub ConditionalFormatting()
Num = ActiveSheet.Range("AD108").Value + 108

Dim MyRange As Range
Dim MyRange2 As Range
Dim MyRange3 As Range
Dim MyRange4 As Range

Set MyRange = Range("AE111:AP" & Num)
Set MyRange2 = Range("AE111:AI" & Num)
Set MyRange3 = Range("AJ111:AJ" & Num)
Set MyRange4 = Range("AK111:AP" & Num)

MyRange.FormatConditions.Delete

MyRange.FormatConditions.Add Type:=xlExpression, Formula1:="=IF($AA111=""Title"",TRUE)"
MyRange.FormatConditions(1).Interior.Color = RGB(198, 224, 180)
MyRange.FormatConditions(1).Font.Color = RGB(68, 104, 86)
MyRange.FormatConditions(1).Font.Bold = True
'MyRange.FormatConditions(1).Borders.Color = RGB(68, 104, 86)
MyRange.FormatConditions(1).StopIfTrue = False

MyRange2.FormatConditions.Add Type:=xlExpression, Formula1:="=IF($AA111=""Rate"",TRUE)"
MyRange2.FormatConditions(2).Interior.Color = RGB(230, 240, 225)
MyRange2.FormatConditions(2).Font.Color = RGB(68, 104, 86)
MyRange2.FormatConditions(2).Borders.Color = RGB(68, 104, 86)
MyRange2.FormatConditions(2).StopIfTrue = False

'it worked great until i added these?

MyRange3.FormatConditions.Add Type:=xlExpression, Formula1:="=IF($AA111=""Rate"",TRUE)"
MyRange3.FormatConditions(3).Interior.Color = RGB(255, 255, 255)
MyRange3.FormatConditions(3).Font.Color = RGB(68, 104, 86)
MyRange3.FormatConditions(3).Borders.Color = RGB(68, 104, 86)
MyRange3.FormatConditions(3).StopIfTrue = False

MyRange4.FormatConditions.Add Type:=xlExpression, Formula1:="=IF($AA111=""Rate"",TRUE)"
MyRange4.FormatConditions(1).Interior.Color = RGB(230, 240, 225)
MyRange4.FormatConditions(1).Font.Color = RGB(68, 104, 86)
MyRange4.FormatConditions(1).Borders.Color = RGB(68, 104, 86)
MyRange4.FormatConditions(1).StopIfTrue = False



End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Your Format condition doesn't add borders, so there is no .FormatConditions(1).Borders to color.
 
Upvote 0
Not working as in
Runtime error?
Does nothing?
Does something but not what is expected?
Something else?

Noting that you're setting condition 3 on MyRange3, it there are less than 3 conditions on that range then that might cause an error (not sure without setting up a test).

As you're adding new conditions but not deleting existing ones, you might not necessarily be setting the rules to the correct condition. From memory, new rules will have the highest index number by default.

If that is the problem then you could use Myrange3.FormatConditions.Count to force the code to work with the newly added rule.
 
Upvote 0
Solution
Hi Jason75,
Thasts it thanks you :)
thank you to everyone for trying to help
now fixed
Tony
 
Upvote 0
Your Format condition doesn't add borders, so there is no .FormatConditions(1).Borders to color.
Do the borders need to be specifically added in CF, or would it colour any existing standard format borders?
I don't think I've ever had cause to use borders with CF, but thought that this would be how it would work.

@tonywatsonhelp

If you've been running the code multiple times then it might be a good idea to do a clean up on your CF rules for those ranges. As you're adding new rules without deleting old ones, you could be building up a quite a collection of duplicate rules.
 
Upvote 0
It should create a border around the cell with the specified colour, regardless of whether there is an existing border or not.
 
Upvote 0
Do the borders need to be specifically added in CF, or would it colour any existing standard format borders?
I don't think I've ever had cause to use borders with CF, but thought that this would be how it would work.

@tonywatsonhelp

If you've been running the code multiple times then it might be a good idea to do a clean up on your CF rules for those ranges. As you're adding new rules without deleting old ones, you could be building up a quite a collection of duplicate rules.
AFAIK, Conditional Formatting either adds the borders specified or it doesn't address borders. "Color the existing borders, what ever they may be" is not an option.
 
Upvote 0

Forum statistics

Threads
1,214,535
Messages
6,120,093
Members
448,944
Latest member
SarahSomethingExcel100

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