I have a pivot table with caculated item. Each time, when I change the parameter in the pivot main filter, text formatting goes back do default.
In order not ot format the table each time, I created a macro, that should format the pivot table whenever I put a value in cell D1. C2 is the format sample cell. It appears that macro runs, but doesn't execute.
Could You help?
The code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$d$1" Then
Application.EnableEvents = False
Range("C2").Activate
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=ORAZ(JEŻELI($B2=" 'OFERTA/TOTAL RYNEK'";1;0);JEŻELI(C2>1;1;0))"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16776961
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.399945066682943
End With
Selection.FormatConditions(1).StopIfTrue = False
Range("C2").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=ORAZ(JEŻELI($B1=""OFERTA/TOTAL RYNEK"";1;0);JEŻELI(C1>1;1;0))"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16776961
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.399945066682943
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.Copy
Columns("C:N").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Application.EnableEvents = True
End If
End Sub
Comparing two strings, such as Target.Address
and "$d$1"
using the following:
If Target.Address = "$d$1" Then
.. can never equate to TRUE
, as if Target.Address
did refer to column 4, row 1, it would hold the string "$D$1"
. Using =
to compare essentially uses Binary Compare.
You either need to correct it to "$D$1"
, or use a Text Compare method with the StrComp
function.
Stepping through the event, and hovering over the Target.Address
would have highlighted this.