Highlight Minimum values in a pivot table

gingerafro

Active Member
Joined
Mar 23, 2005
Messages
448
I have a very large data set (about 450k rows) of hotels with a price for each day for a whole year.

I have put them in a pivot table with dates down the left and hotels across the top. price is obviously the data field.

I want to show for each day of the year, which hotel is the cheapest by highlighting it. I have tried running a macro and the basics of it are below.

Sub dateselect()

Dim d As PivotItem
Dim b As PivotItem

With Worksheets("Sheet1").PivotTables("PivotTable5")
For Each d In .PivotFields("START DATE").PivotItems
For Each b In .PivotFields("NIGHT PRICE (MKT CURR)").PivotItems
If b = Min(d) Then
.PivotItems(b).Font.Color = 2
End If
Next
Next
End With
End Sub

If I was doing this manually, I would look down at each date then look across to find the cheapest price. I had hoped my macro would do it, but my VBA writing is a bit 'pigeon'.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
You could use Conditional Formatting:

Code:
Sub Test()
    With Worksheets("Sheet1").PivotTables("PivotTable5").DataBodyRange
        .FormatConditions.Delete
        .FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=" & .Cells(1, 1).Address(False, False) & "=MIN(" & Range(.Cells(1, 1), .Cells(1, .Columns.Count)).Address(False, True) & ")"
        .FormatConditions(1).Interior.ColorIndex = 35
    End With
End Sub
 
Upvote 0
Hi Andrew

For this to work properly, I need to select the top left corner of the pivot table. Can this be done automatically?

thank you!
 
Upvote 0
I'm not trying to select anything. The code only works if the cell highlighted at the time of running the macro is the top left of the pivot's data body.

If the cell is in the middle of the pivot all sorts of cells get highlighted. I think it is something to do with the 'address' part of the code.
 
Upvote 0
That seems to be a bug, because Formula1 is a text expression that should get entered as is. Try:

Code:
Sub Test()
    With Worksheets("Sheet1").PivotTables("PivotTable5").DataBodyRange
        .Cells(1, 1).Select
        .FormatConditions.Delete
        .FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=" & .Cells(1, 1).Address(False, False) & "=MIN(" & Range(.Cells(1, 1), .Cells(1, .Columns.Count)).Address(False, True) & ")"
        .FormatConditions(1).Interior.ColorIndex = 35
    End With
End Sub
 
Upvote 0
Hi Andrew,

Its not pretty, and therefore probably not the best way, but I have added to your code and it is working.

Sub Test()

Worksheets("Sheet1").Range("A1:IV65535").FormatConditions.Delete

Worksheets("Sheet1").PivotTables("PivotTable5").DataBodyRange.Select

With Worksheets("Sheet1").PivotTables("PivotTable5").DataBodyRange
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=" & .Cells(1, 1).Address(False, False) & "=MIN(" & Range(.Cells(1, 1), .Cells(1, .Columns.Count)).Address(False, True) & ")"
.FormatConditions(1).Interior.ColorIndex = 35
End With
End Sub


thank you for your time and help.
ps. when I get a minute, I'll have a think about why it didn't work first off. I agree with you that as you chose the pivot's DataBodyRange it should have worked.
 
Upvote 0

Forum statistics

Threads
1,221,417
Messages
6,159,789
Members
451,589
Latest member
Harold14

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