Solved

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

Posted on 2013-11-08
13
650 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
 

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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Office 2016 User Guides 5 31
Adding Text that self adjusts in a Cell 8 31
splitting text of cell to columns 14 22
Most Consistent Performer 4 19
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

947 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now