简体   繁体   中英

Conditional Formatting Pivot Table based on Dynamic Column Headers

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,eg 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://imgur.com/s6cQ08L )

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.

You can do something like this:

Dim c As Range, sht As Worksheet, f as string

Set sht = Activesheet

Set c = sht.Cells(4, rngDateHeader.Column) 'eg B4

f = "=AND(" & c.address(false, false) & "=" & _
     c.offset(1,0).address(false, false) & ",D4<>D5)"  '<< use formula f in your CF

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM