Solved

# Lookup pivot and highlight

Posted on 2012-03-27
253 Views
Last Modified: 2012-03-28
Hi,

Im loking for some code that will look at the data in ColA in one pivot and if there is a matching entry in ColA in a pivot on another tab, highlight the whole row green, if it is not a  match, highlight it red.

I did the first as an example

Thanks
Seamus
test.zip
0
Question by:Seamus2626
• 2
• 2
4 Comments

LVL 41

Accepted Solution

dlmille earned 500 total points
ID: 37772470
I would use this code:

``````Sub matchPivots()
Dim wkb As Workbook
Dim wksA As Worksheet
Dim wksB As Worksheet
Dim rng As Range
Dim r As Range
Dim rFind As Range
Dim rColor As Range

Set wkb = ThisWorkbook
Set wksA = wkb.Sheets("ASPA Pivot")
Set wksB = wkb.Sheets("Sophis Pivot")

Set rng = wksA.Range("A5", wksA.Range("A" & Rows.Count).End(xlUp).Offset(-1, 0))

For Each r In rng
Set rColor = wksA.Range(r, wksA.Cells(r.Row, "G"))
Set rFind = wksB.Range("A:A").Find(what:=r.Value, LookIn:=xlValues, lookat:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext)
If Not rFind Is Nothing Then
rColor.Interior.Color = vbGreen
Else
rColor.Interior.Color = vbRed
End If
Next r

End Sub
``````

See attached.

Dave
match-pivots-r1.xls
0

Author Closing Comment

ID: 37775468
Thanks Dave!

Seamus
0

Author Comment

ID: 37776037
One question Dave, the colours expand to col G, however the pivots shrink and expand with new data. There is always a grand total for for Columns, so could you amend so the colouring only goes as far as "Grand Total"

Thanks
Seamus
0

LVL 41

Expert Comment

ID: 37777372
Sure.  It searches for "Grand Total" on row 4, if not found, then uses last column on row 4.  Change the row number if you move the pivot table:

``````Option Explicit

Sub matchPivots()
Dim wkb As Workbook
Dim wksA As Worksheet
Dim wksB As Worksheet
Dim rng As Range
Dim r As Range
Dim rFind As Range
Dim rColor As Range
Dim lastCol As Long

Set wkb = ThisWorkbook
Set wksA = wkb.Sheets("ASPA Pivot")
Set wksB = wkb.Sheets("Sophis Pivot")

Set rng = wksA.Range("A5", wksA.Range("A" & Rows.Count).End(xlUp).Offset(-1, 0))

On Error Resume Next
lastCol = wksA.Range("4:4").Find(what:="Grand Total", LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByColumns, searchdirection:=xlNext).Column
If lastCol = 0 Then
lastCol = wksA.Cells(4, Columns.Count).End(xlToLeft).Column
End If
On Error Resume Next

For Each r In rng
Set rColor = wksA.Range(r, wksA.Cells(r.Row, lastCol))
Set rFind = wksB.Range("A:A").Find(what:=r.Value, LookIn:=xlValues, lookat:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext)
If Not rFind Is Nothing Then
rColor.Interior.Color = vbGreen
Else
rColor.Interior.Color = vbRed
End If
Next r

End Sub
``````

See attached.

Dave
match-pivots-r2.xls
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

#### 776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.