Conditional Formatting Formula Help

larinda4

Board Regular
Joined
Nov 15, 2021
Messages
73
Office Version
  1. 365
Platform
  1. Windows
Good morning,

I'm having an issue with setting the formula for my conditional format.

Some background information: I receive a report and copy and paste as values to the right hand side of it. Then I update all the figures with formulas to pull in correct numbers from various tabs. I want to compare the numbers to my original and highlight any cells that do not match yellow.

Here is my code:
VBA Code:
'Define Range
Dim CombinedRange As Range
Dim FirstRange As Range
Dim SecondRange As Range

Set CombinedRange = Range("U" & FirstRow3 & ":X" & LastRow3 & " ")
Set FirstRange = Range("U" & FirstRow & ":X" & LastRow & " ")
Set SecondRange = Range("U" & FirstRow2 & ":X" & LastRow2 & " ")

'Delete Existing Conditional Formatting from Range
CombinedRange.FormatConditions.Delete
FirstRange.FormatConditions.Delete
SecondRange.FormatConditions.Delete

'Apply Conditional Formatting
CombinedRange.FormatConditions.Add Type:=xlExpression, Formula1:="=ROUND(U8,2)<>ROUND(G8,2)"
FirstRange.FormatConditions.Add Type:=xlExpression, Formula1:="=ROUND(U48,2)<>ROUND(G48,2)"
SecondRange.FormatConditions.Add Type:=xlExpression, Formula1:="=ROUND(U88,2)<>ROUND(G88,2)"

'Set interior color to red
CombinedRange.FormatConditions(1).Interior.Color = RGB(255, 255, 102)
FirstRange.FormatConditions(1).Interior.Color = RGB(255, 255, 102)
SecondRange.FormatConditions(1).Interior.Color = RGB(255, 255, 102)

My issue is here:
CombinedRange.FormatConditions.Add Type:=xlExpression, Formula1:="=ROUND(U8,2)<>ROUND(G8,2)"
FirstRange.FormatConditions.Add Type:=xlExpression, Formula1:="=ROUND(U48,2)<>ROUND(G48,2)"
SecondRange.FormatConditions.Add Type:=xlExpression, Formula1:="=ROUND(U88,2)<>ROUND(G88,2)"

The reports can be monthly, quarterly or annually so those cells change every time. How can I make the formula dynamic? Any help is appreciated!
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
How can I make the formula dynamic?

Try the following:

The format starts at cell U8 and ends at the last cell with data in columns G through X.
The formatting is applied to column U:
VBA Code:
Sub Conditional_Formatting()
  Range("U:U").FormatConditions.Delete
  With Range("U8:U" & Range("G:X").Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row)
    .FormatConditions.Add Type:=xlExpression, Formula1:="=ROUND($U8,2)<>ROUND($G8,2)"
    .FormatConditions(1).Interior.Color = RGB(255, 255, 102)
    .FormatConditions(1).ModifyAppliesToRange .Cells
  End With
End Sub


But if you want it to apply up to column X, then change to this:
VBA Code:
Sub Conditional_Formatting()
  Range("U:X").FormatConditions.Delete
  With Range("U8:U" & Range("G:X").Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row)
    .FormatConditions.Add Type:=xlExpression, Formula1:="=REDONDEAR($U8,2)<>REDONDEAR($G8,2)"
    .FormatConditions(1).Interior.Color = RGB(255, 255, 102)
    .FormatConditions(1).ModifyAppliesToRange .Cells.Resize(.Rows.Count, 4)
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

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