thanks for giving a newbie in VBA a little help and guidance.
I am trying to program a macro for conditional formatting in a pivot table(s).
Goal is to find two columns by headers (as their location might change) and apply a conditional formula to one of them. (highlight outlier cells in red)
Headers are in Row 3
Columns starting with A
Example below:
I want to find the column that has Origin Airport,
then find the column that has Origin Handling,
create a formula the has 2 conditions to compare,e.g. B4 with B5 (match) and D4 with D5 (match) and then highlight any values in Origin Handling (column D) that are not in line, where D4 <> D5.
The following formula I used to test for consistency "=(AND($B4=$B5,D4<>D5)"
! [screenshot of the table] (https://i.stack.imgur.com/fhNog.jpg)
I have gone through various posts here on SO and put the codes together that i think are applicable. Being a newbie however I am not getting the correct flow, was trying segment by segment but hit a wall when trying to find a code that provides column Letter by find: Header and then creates a formula. Below code sequence is incomplete.
Sub Origin_Charges
' Review origin charges based on Consistency
With ActiveSheet.PivotTables("Origin") 'focuses on the Pivot Table I named Origin'
Dim ws as worksheet
Dim rngDateHeader As Range
Dim rngHeaders As Range
End with
Set rngHeaders = Range("3:3") 'Looks in entire first row; adjust as needed.
Set rngDateHeader = rngHeaders.Find("Origin Airport")
If rngDateHeader is Nothing then "No Origin Airport Column Found"
Set rngHeaders = Range("3:3") 'Looks in entire first row; adjust as needed.
Set rngDateHeader = rngHeaders.Find("Origin Handling")
If rngDateHeader is Nothing then "No Origin Handling Column Found"
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND($B4=$B5,D4<>D5)" 'Formula right now is fixed but want it to adjust based on what column Origin Airport is in'
Selection.FormatConditions(Selection.
FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
With Sheets("Origin")
.Range("D4").AutoFill .Range("D4:D" & .Cells(.Rows.count,"C")
.End(xlUp).row)
End With
End Sub
Any help is greatly appreciated.