Highlight columns in pivot table based on grouped dates

chally3

Board Regular
Joined
Mar 22, 2006
Messages
157
Good morning all,

Ive been searching the net to find an answer to see if this is possible and have drawn a blank.
I have a pivot table that groups data by "qty due" & "date due" (grouped into working weeks across the columns) and was wandering if its possible to highlight the columns that are l<'end of current working week' and also highlight the columns that are >"end of the current working week" and <="end of the following working week"
Column D , row 4 is the first consolidated week columns (number of rows vary depending on the date extracted which I pivot.

Grouping by weeks is essential


Any help appreciated as ever. Thank you
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Good evening

The example below highlights columns with dates earlier than the current week. It assumes column labels in the format “01/08/18 – 08/08/18”.

Code:
Sub HLight()
Dim pt As PivotTable, r As Range, i%, rn%
Set pt = ActiveSheet.PivotTables("pivot4")      ' your table name here
Set r = pt.ColumnRange
rn = Split(pt.DataBodyRange.Address, "$")(4)
For i = 1 To r.Columns.Count - 1
    If WorksheetFunction.WeekNum(CDate(Split(r.Cells(2, i), "-")(1))) - _
    WorksheetFunction.WeekNum(Now) < 0 Then _
    Range(r.Cells(2, i), Cells(rn, r.Cells(2, i).Column)).Interior.Color = vbYellow
Next
End Sub
 
Last edited:
Upvote 0
Good evening

The example below highlights columns with dates earlier than the current week. It assumes column labels in the format “01/08/18 – 08/08/18”.

Code:
Sub HLight()
Dim pt As PivotTable, r As Range, i%, rn%
Set pt = ActiveSheet.PivotTables("pivot4")      ' your table name here
Set r = pt.ColumnRange
rn = Split(pt.DataBodyRange.Address, "$")(4)
For i = 1 To r.Columns.Count - 1
    If WorksheetFunction.WeekNum(CDate(Split(r.Cells(2, i), "-")(1))) - _
    WorksheetFunction.WeekNum(Now) < 0 Then _
    Range(r.Cells(2, i), Cells(rn, r.Cells(2, i).Column)).Interior.Color = vbYellow
Next
End Sub


A diamond sir
 
Upvote 0
Hi Worf,

Ive tried pasting the VBA and changed Set pt = ActiveSheet.PivotTables("pivot4") to Set pt = ActiveSheet.PivotTables("Stands") and I get a Runtime error '1004'; application-defined or object defined error.
Any suggestions?

Thank you
Mark
 
Upvote 0
Hi Mark

Try the code below and see if “Stands” appears. Note that the first version refers to the active sheet.


Code:
 Sub PTables()
Dim ws As Worksheet, i%
Set ws = Sheets("data")             ' your sheet name here
For i = 1 To ws.PivotTables.Count
    MsgBox ws.PivotTables(i).Name
Next
End Sub
 
Upvote 0
Hi Worf, apologies for the delay, I've just had time to trial the code.
All I see when I run the code is 2 pop up boxes, saying pivottable4 & then pivottable3. No columns are highlighted :(
 
Upvote 0
New version:

Code:
Sub HLight()
Dim pt As PivotTable, r As Range, i%, ws As Worksheet
Set ws = Sheets("data")
Set pt = ws.PivotTables("pivot4")      ' replace both names here
Set r = pt.ColumnRange
For i = 1 To r.Columns.Count - 1
    If WorksheetFunction.WeekNum(CDate(Split(r.Cells(2, i), "-")(1))) - _
    WorksheetFunction.WeekNum(Now) < 0 Then _
    ws.Range(r.Cells(2, i), ws.Cells(Split(pt.DataBodyRange.Address, "$")(4), _
    r.Cells(2, i).Column)).Interior.Color = vbYellow
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

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