I want the last cell in column L that has a number to be green if it is a larger number than cell D11
Name: | LastRow |
Refers to: | =MATCH(9.9E+307,$L:$L) |
Formula: | =AND(L1<$D$11,ROW()=LastRow) | Refers to: | =$L:$L | Format: | Red fill |
Formula: | =AND(L1>$D$11,ROW()=LastRow) | Refers to: | =$L:$L | Format: | Green fill |
Book1 | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
D | L | ||||||||||
1 | Numbers | ||||||||||
2 | 45 | ||||||||||
3 | 34 | ||||||||||
4 | 23 | ||||||||||
5 | |||||||||||
6 | |||||||||||
7 | |||||||||||
8 | 4 | ||||||||||
9 | 33 | ||||||||||
10 | 44 | ||||||||||
11 | 48 | ||||||||||
Green Red |
Cells with Conditional Formatting | ||||
---|---|---|---|---|
Cell | Condition | Cell Format | Stop If True | |
L1:L20 | Expression | =AND(ISNUMBER(L1),COUNT(L1:L$10000)=1,L1<$D$11) | text | NO |
L1:L20 | Expression | =AND(ISNUMBER(L1),COUNT(L1:L$10000)=1,L1>$D$11) | text | NO |
You have to define the LastRow name and the Conditional Formatting rules on each sheet, which this macro does:This worked great thank you very much.
I have the same problem with 24 other sheets in the same workbook. I tried to apply the formatting rules to another sheet (without knowing I had to start over with defining a name because I have never done anything like this before) and the colored cell was two rows down from the last row with a number. But the color was correct. Then I thought I should go thru the same procedure you wrote. So I First defined the name etc. Sorry but is there a way to make a global ( I do not know what I am talking about) workbook wide something or another? If not then thanks again.
Public Sub Create_CF_Rules()
Dim ws As Worksheet
Dim LastRow As Name
For Each ws In ActiveWorkbook.Worksheets
'Add or update LastRow name on this sheet
On Error Resume Next
Set LastRow = ws.Names("LastRow")
On Error GoTo 0
If LastRow Is Nothing Then
ws.Names.Add Name:="LastRow", RefersTo:="=MATCH(9.9E+307,$L:$L)"
Else
LastRow.RefersTo = "=MATCH(9.9E+307,$L:$L)"
End If
'Add or update Conditional Formatting rules in column L on this sheet
With ws.Columns("L:L")
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:="=AND(L1<$D$11,ROW()=LastRow)"
With .FormatConditions(.FormatConditions.Count)
.SetFirstPriority
.StopIfTrue = False
With .Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
End With
.FormatConditions.Add Type:=xlExpression, Formula1:="=AND(L1>$D$11,ROW()=LastRow)"
With .FormatConditions(.FormatConditions.Count)
.SetFirstPriority
.StopIfTrue = False
With .Interior
.PatternColorIndex = xlAutomatic
.Color = 5287936
.TintAndShade = 0
End With
End With
End With
Next
End Sub
ws.Columns("L:L")
to ws.Range("L1:L10000")
and change the Formula1 strings to Peter's formulae.Public Sub Create_CF_Rules()
Dim ws As Worksheet
Dim LastRow As Name
For Each ws In ActiveWorkbook.Worksheets
If ws.Name Like "[A-Z][A-Z][A-Z]-[A-Z][A-Z][A-Z]" Then
'Add or update LastRow name on this sheet
On Error Resume Next
Set LastRow = ws.Names("LastRow")
On Error GoTo 0
If LastRow Is Nothing Then
ws.Names.Add Name:="LastRow", RefersTo:="=MATCH(9.9E+307,$L:$L)"
Else
LastRow.RefersTo = "=MATCH(9.9E+307,$L:$L)"
End If
'Add or update Conditional Formatting rules in column L on this sheet
With ws.Columns("L:L")
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:="=AND(L1<$D$11,ROW()=LastRow)"
With .FormatConditions(.FormatConditions.Count)
.SetFirstPriority
.StopIfTrue = False
With .Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
End With
.FormatConditions.Add Type:=xlExpression, Formula1:="=AND(L1>$D$11,ROW()=LastRow)"
With .FormatConditions(.FormatConditions.Count)
.SetFirstPriority
.StopIfTrue = False
With .Interior
.PatternColorIndex = xlAutomatic
.Color = 5287936
.TintAndShade = 0
End With
End With
End With
End If
Next
End Sub