Nuke_It_Newport
New Member
- Joined
- Nov 17, 2020
- Messages
- 47
- Office Version
- 365
- Platform
- Windows
Happy Sunday everyone-
I have the following code that sets conditional formatting rules for the currently selected row and cell. When the user selects a different row or cell, I need to delete the CF rules and apply a new set of rules.
What is happening is the CF rules are applying correctly for the active row and cell, but when the user selects a different row or cell, the new rules are not applying correctly. The following CF rules are applied no matter what the value is in the helper column that the CF rule is to reference. In other words, the row that was previously selected gets formatted to a font color of gray with strikethrough per the following code. It should format according to the color in the helper column based on the subsequent code in the module, as see in the above code snippet.
Can anybody give me some guidance?
Thanks!
I have the following code that sets conditional formatting rules for the currently selected row and cell. When the user selects a different row or cell, I need to delete the CF rules and apply a new set of rules.
VBA Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Dim listObj As ListObject
Dim prevRange As Range
Dim ws As Worksheet
'Check if target is inside table
If Intersect(ActiveCell, ActiveSheet.ListObjects(1).DataBodyRange) Is Nothing Then Exit Sub
If prevRange Is Nothing Then
Set prevRange = Target
Exit Sub
End If
Set ws = ActiveSheet
Set listObj = ws.ListObjects(1)
'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
'Format Active Cell
'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
With Target.FormatConditions.Add(Type:=xlExpression, Formula1:="=TRUE")
.StopIfTrue = False
With .Interior
.Color = clrLtGray
.TintAndShade = 0
End With
End With
'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
'Format Active Row
'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
With Intersect(ActiveCell.EntireRow, listObj.DataBodyRange).FormatConditions.Add(Type:=xlExpression, Formula1:="=TRUE")
.StopIfTrue = False
With .Interior
.Color = clrLtYellow
.TintAndShade = 0
End With
End With
'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
'Set font color for previously selected row (entire table row) based on value in Col "T" of same row
''$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
With Intersect(prevRange.EntireRow, listObj.DataBodyRange)
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:="=$T5=""Gray"""
With .FormatConditions(1)
.StopIfTrue = False
With .Font
.Bold = False
.Italic = False
.Strikethrough = True
.Color = clrGray
.TintAndShade = 0
End With
End With
End With
'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
'Set font color for previously selected row, column 1, based on color in column "T" of same row
'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
With Intersect(prevRange.EntireRow, listObj.ListColumns(1).DataBodyRange)
.FormatConditions.Add Type:=xlExpression, Formula1:="=$T5=""Black"""
With .FormatConditions(2)
.StopIfTrue = False
With .Font
.Bold = False
.Italic = False
.Strikethrough = False
.Color = clrBlack
.TintAndShade = 0
End With
End With
.FormatConditions.Add Type:=xlExpression, Formula1:="=$T5=""Red"""
With .FormatConditions(3)
.StopIfTrue = False
With .Font
.Bold = False
.Italic = False
.Strikethrough = False
.Color = clrRed
.TintAndShade = 0
End With
End With
.FormatConditions.Add Type:=xlExpression, Formula1:="=$T5=""Green"""
With .FormatConditions(4)
.StopIfTrue = False
With .Font
.Bold = False
.Italic = False
.Strikethrough = False
.Color = clrGreen
.TintAndShade = 0
End With
End With
.FormatConditions.Add Type:=xlExpression, Formula1:="=$T5=""Orange"""
With .FormatConditions(5)
.StopIfTrue = False
With .Font
.Bold = False
.Italic = False
.Strikethrough = False
.Color = clrOrange
.TintAndShade = 0
End With
End With
End With
'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
'Set font color for previously selected row, column 5, based on color in column "U" of same row
'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
With Intersect(prevRange.EntireRow, listObj.ListColumns(5).DataBodyRange)
.FormatConditions.Add Type:=xlExpression, Formula1:="=$U5=""Black"""
With .FormatConditions(2)
.StopIfTrue = False
With .Font
.Bold = False
.Italic = False
.Strikethrough = False
.Color = clrBlack
.TintAndShade = 0
End With
End With
.FormatConditions.Add Type:=xlExpression, Formula1:="=$U5=""Red"""
With .FormatConditions(3)
.StopIfTrue = False
With .Font
.Bold = False
.Italic = False
.Strikethrough = False
.Color = clrRed
.TintAndShade = 0
End With
End With
.FormatConditions.Add Type:=xlExpression, Formula1:="=$U5=""Green"""
With .FormatConditions(4)
.StopIfTrue = False
With .Font
.Bold = False
.Italic = False
.Strikethrough = False
.Color = clrGreen
.TintAndShade = 0
End With
End With
.FormatConditions.Add Type:=xlExpression, Formula1:="=$U5=""Orange"""
With .FormatConditions(5)
.StopIfTrue = False
With .Font
.Bold = False
.Italic = False
.Strikethrough = False
.Color = clrOrange
.TintAndShade = 0
End With
End With
End With
'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
'Set font color for previously selected row, column 11, based on color in column "V" of same row
'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
With Intersect(prevRange.EntireRow, listObj.ListColumns(11).DataBodyRange)
.FormatConditions.Add Type:=xlExpression, Formula1:="=$V5=""Black"""
With .FormatConditions(2)
.StopIfTrue = False
With .Font
.Bold = False
.Italic = False
.Strikethrough = False
.Color = clrBlack
.TintAndShade = 0
End With
End With
.FormatConditions.Add Type:=xlExpression, Formula1:="=$V5=""Red"""
With .FormatConditions(3)
.StopIfTrue = False
With .Font
.Bold = False
.Italic = False
.Strikethrough = False
.Color = clrRed
.TintAndShade = 0
End With
End With
.FormatConditions.Add Type:=xlExpression, Formula1:="=$V5=""Green"""
With .FormatConditions(4)
.StopIfTrue = False
With .Font
.Bold = False
.Italic = False
.Strikethrough = False
.Color = clrGreen
.TintAndShade = 0
End With
End With
.FormatConditions.Add Type:=xlExpression, Formula1:="=$V5=""Orange"""
With .FormatConditions(5)
.StopIfTrue = False
With .Font
.Bold = False
.Italic = False
.Strikethrough = False
.Color = clrOrange
.TintAndShade = 0
End With
End With
End With
Set prevRange = Target
End Sub
What is happening is the CF rules are applying correctly for the active row and cell, but when the user selects a different row or cell, the new rules are not applying correctly. The following CF rules are applied no matter what the value is in the helper column that the CF rule is to reference. In other words, the row that was previously selected gets formatted to a font color of gray with strikethrough per the following code. It should format according to the color in the helper column based on the subsequent code in the module, as see in the above code snippet.
Code:
'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
'Set font color for previously selected row (entire table row) based on value in Col "T" of same row
''$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
With Intersect(prevRange.EntireRow, listObj.DataBodyRange)
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:="=$T5=""Gray"""
With .FormatConditions(1)
.StopIfTrue = False
With .Font
.Bold = False
.Italic = False
.Strikethrough = True
.Color = clrGray
.TintAndShade = 0
End With
End With
End With
Can anybody give me some guidance?
Thanks!