VBA / Pivot Table / Comparing values from two fields and highlighting

LEXCERM

Active Member
Joined
Jun 26, 2004
Messages
320
Office Version
  1. 365
Platform
  1. Windows
Hi,

Hoping you can help.

From the example Pivot (below), I need to highlight any [Country] that does not equal GB and has a [Confirmed] status of YES (bearing in mind we deal with many different countries so hardcoding each country is not an option):-

1669215099814.png


I've tried using many different codes and although I am able to highlight specific values for one PivotField, I am unable to work out how to compare different fields within a Pivot.

Here are a couple of snippets of what I have been working with which has changed a multitude of times:-

If Worksheets("Sheet1").PivotTables("PivotTable1").PivotFields("Confirmed").PivotItems("YES") = "YES" _
And Worksheets("Sheet1").PivotTables("PivotTable1").PivotFields("Country").PivotItems("GB") <> "GB" Then
Worksheets("Sheet1").PivotTables("PivotTable1").PivotFields("Country").PivotItems("GB").LabelRange.Interior.Color = vbYellow
End If

------------------------

Dim PvtTbl As PivotTable
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Dim rng4 As Range

Set PvtTbl = ActiveSheet.PivotTables("PivotTable1")
Set rng1 = PvtTbl.PivotFields("Confirmed").DataRange
Set rng2 = PvtTbl.PivotFields("Confirmed").PivotItems("YES").LabelRange
Set rng3 = PvtTbl.PivotFields("Country").DataRange
Set rng4 = PvtTbl.PivotFields("Country").PivotItems("GB").LabelRange

If Intersect(rng2, rng4) Is Nothing Then
Intersect(rng3, rng4).Interior.Color = vbYellow
End If


Thanks in advance for your help. :)
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi,

Hoping you can help.

From the example Pivot (below), I need to highlight any [Country] that does not equal GB and has a [Confirmed] status of YES (bearing in mind we deal with many different countries so hardcoding each country is not an option):-

View attachment 79348

I've tried using many different codes and although I am able to highlight specific values for one PivotField, I am unable to work out how to compare different fields within a Pivot.

Here are a couple of snippets of what I have been working with which has changed a multitude of times:-

If Worksheets("Sheet1").PivotTables("PivotTable1").PivotFields("Confirmed").PivotItems("YES") = "YES" _
And Worksheets("Sheet1").PivotTables("PivotTable1").PivotFields("Country").PivotItems("GB") <> "GB" Then
Worksheets("Sheet1").PivotTables("PivotTable1").PivotFields("Country").PivotItems("GB").LabelRange.Interior.Color = vbYellow
End If

------------------------

Dim PvtTbl As PivotTable
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Dim rng4 As Range

Set PvtTbl = ActiveSheet.PivotTables("PivotTable1")
Set rng1 = PvtTbl.PivotFields("Confirmed").DataRange
Set rng2 = PvtTbl.PivotFields("Confirmed").PivotItems("YES").LabelRange
Set rng3 = PvtTbl.PivotFields("Country").DataRange
Set rng4 = PvtTbl.PivotFields("Country").PivotItems("GB").LabelRange

If Intersect(rng2, rng4) Is Nothing Then
Intersect(rng3, rng4).Interior.Color = vbYellow
End If


Thanks in advance for your help. :)

So I am getting closer, and knocked-up this code which isn't quite right-

Dim PvtTbl As PivotTable
Dim PvtFld As PivotField
Dim PvtFld2 As PivotField
Dim PvtItm As PivotItem
Dim PvtItm2 As PivotItem

Set PvtTbl = ActiveSheet.PivotTables("PivotTable1")

Set PvtFld = PvtTbl.PivotFields("Confirmed")
Set PvtFld2 = PvtTbl.PivotFields("Country")

For Each PvtItm In PvtFld.PivotItems
For Each PvtItm2 In PvtFld2.PivotItems

If PvtItm.Name = "YES" And PvtItm2.Name <> "GB" Then
PvtItm2.DataRange.Interior.Color = vbRed
End If
Next PvtItm2

Next PvtItm


Even though PvtItm.Name and PvtItm2.Name are picking up ="YES" and <>"GB", it colours in the "NO" values as well:-

1669291408116.png


Thanks for listening! :)
 
Upvote 0
So the code is highlighting just <>"GB" and excluding the "Confirmed" argument of "YES", hmmmm:-
 

Attachments

  • 1669293005043.png
    1669293005043.png
    18.7 KB · Views: 16
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,300
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top