Solved

Excel vba to apply formats to cells in a pivot table based on a value

Posted on 2013-11-08
13
655 Views
Last Modified: 2013-11-14
Hi,

I have a pivot table that consists of a number of columns and 1 row label
The row label is a date.

Basically I want to change the color of the row base on the value in the row label.
The number of rows can change on refresh. The columns are constant
The 1st row is always 'Late last month'
The last row is always 'Future Next Month'


i.e

Row    Column (B)                   Column (D)

                 
3        07/11/2013                    (cell B3  outside pivot table)


          provottable1
         Due Date                        Val1           
8       Late Last Month                   1
9        27/10/2013                       1
10      28/10/2013                       2
11      29/10/2013                       4
12      30/10/2013                       0
13      31/10/2013                       8
14      01/11/2013                       0
15      02/11/2013                       1
16      03/11/2013                       4
17      04/11/2013                       4
18      05/11/2013                       0
19      06/11/2013                       9
20      07/11/2013                       0
21      08/11/2013                       6
21      09/11/2013                       4
24      10/11/2013                       5
25      11/11/2013                       4
26      12/11/2013                       8
27      13/11/2013                       5
28      14/11/2013                       5
29      15/11/2013                       6
30      16/11/2013                       4
31      17/11/2013                       5
32      18/11/2013                       3
33      19/11/2013                       4
34      20/11/2013                       8
35      21/11/2013                       7
36      22/11/2013                       2
37      23/11/2013                       4
38      Future Next Month         12        


I see the steps as being as follows but I'm not particularly good at VBA

1.       Enter the Table and clear any existing formats.

2.       find the last row that needs to to formatted in the pivottable.
         Go to the row that contains 'Future Next Month' in column B of the table
         Loop back until the Due Date is < the date set outside the pivot in cell B3
          (that would  equate to row 19)

3        Highlight from last row found to row 19 back to row 9 .    (columns B and D)

4.       Change the color


I've seen various forums about this but none do what I want and all the examples use different methods that I can't combine.

hope you can help
0
Comment
Question by:EWHTLC
  • 5
  • 5
  • 2
  • +1
13 Comments
 
LVL 16

Expert Comment

by:terencino
ID: 39634916
Have you considered conditional formatting for this task? If you like to send a sample table and the colours you want for the cells than I can show you how it would apply in this situation
...Terry
0
 
LVL 30

Expert Comment

by:hnasr
ID: 39634929
Try to reproduce the issue and upload the sheet.
Give example of how you expect the output.
Example look as cell x, y. The label is label1, I expect the color to be ....
0
 
LVL 14

Expert Comment

by:Faustulus
ID: 39636099
Any solution to your problem will have to be custom made. It will work only on your workbook. Therefore it can't be made without access to your workbook. You can replace proprietary data with dummies of identical data type.
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

Author Comment

by:EWHTLC
ID: 39637320
Yes I've tried condition formatting but it too erratic.
I'm looking for a vba solution

It's a biggy so impossible to post.
I was hoping there was enough information there to have a stab so that I can adopt it to fit.

Aah well, worth a try.
0
 
LVL 14

Expert Comment

by:Faustulus
ID: 39637433
Post the first 20 rows.
0
 

Author Comment

by:EWHTLC
ID: 39637514
Here's an extract of the file.

The pivot is refreshed by users each day a macro to sql connections

I want to change the color of columns B, D,E,F,F to be red only affecting rows where the due date less than the date in cell C2
The values are made up so it's ok to mess around


Cheers
Daily-Order-and-Sales.xlsm
0
 
LVL 16

Expert Comment

by:terencino
ID: 39637588
Those cells are formatted as text not dates, which is why conditional formatting hasn't worked before. Here is an update using conditional formatting attached, using DATEVALUE to convert text to date
...Terry
Daily-Order-and-Sales.xlsm
0
 

Author Comment

by:EWHTLC
ID: 39642723
I did try Conditional formatting but I encountered problems when I did the data refresh and the subsequent pivot refresh.
Each time I refresh the pivot the formatting is lost.
0
 
LVL 16

Accepted Solution

by:
terencino earned 500 total points
ID: 39643260
I see now. Yes the conditional formats are deleted when the table is refreshed, and it all gets messed up. I've done some further research and the approach for pivot tables is different. But I think it is the way to go here, rather than VBA. Can you test the attached update to see if the conditional formats stay after refreshing the table now?
...Terry
Daily-Order-and-Sales-V2.xlsm
0
 

Author Comment

by:EWHTLC
ID: 39645847
Thatg is truly Amazing
You my friend are a wizard
It works a treat.

I an see what you have done but wondered how you got the formats to hold after the refresh.

Thanks
0
 
LVL 16

Expert Comment

by:terencino
ID: 39646460
Great the trick is to apply the conditional formats to individual columns in the values section of the pivot table. So :
1. Select D8, the first value in the first column of the Values section of the pivot table
2. Click on Conditional Formatting > New Rule and select "All cells showing Order Val values for Due Date". This will ignore subtotals and grand totals. Also, it will expand automatically on refresh, as the conditional formatting is locked to the pivot table, not the cells below.
3. Select the Rule Type "Use a formula..." and enter the formula
=DATEVALUE($B8)<DATEVALUE($C$2)
This will be TRUE for the required dates, and FALSE or an error for other entries, which will be ignored. The referencing is less important for these pivot table formats, but essential for the normal range formats, so I have kept it the same throughout.
4. Change the format to suit
5. Repeat for all the other Values columns
6. For the Row Label Due Date, it should be treated the same as a normal range. Just select all the cells starting from B8 to B51 that are likely to be included in a refresh, and select a formula and add the conditional formula above.
Conditional formatting for pivot table
0
 
LVL 16

Expert Comment

by:terencino
ID: 39646468
Also, here is the chapter & verse from Microsoft on using conditional formatting with pivot tables:

Add, change, find, or clear conditional formats
0
 

Author Comment

by:EWHTLC
ID: 39647234
Excellent.
I have tweaked based on what you have done and added formats to other columns.

Thanks you so much for your help.
It is much appreciated.
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

This article will show you how to use shortcut menus in the Access run-time environment.
My experience with Windows 10 over a one year period and suggestions for smooth operation
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

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

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

Join & Ask a Question