0

I have some VBA code that runs a SQL Query, dumps it into an Excel 2010 spreadsheet and refreshes the 2 Pivot Tables based off the data. I also have 2 Pivot Charts based off of the Pivot Tables, and every time the Tables get updated, the formatting on the Charts is lost.

I followed the instructions in Section 12.3 of this book which suggested creating a custom chart type based on my formatting preferences and applying it to the charts that I want to have remain formatted that way. These instructions work great if I manually modify the data then manually refresh the Pivot Table - the Charts remain properly formatted. However, when I execute this code:

For Each pt In NewSheet.PivotTables
  pt.RefreshTable
Next

the formatting is immediately lost.

Here's what the data looks like before:

enter image description here

and my carefully crafted pie charts:

enter image description here

As soon as I update the data and refresh the tables:

enter image description here

This is what I get:

enter image description here

Again, having the custom chart style applied will keep the formatting if done manually, but executing the VBA code somehow resets the custom chart type.

4
  • Short answer-don't use pivot charts. Just use the pivot table data to input into standard charts (either by direct cell references or named ranges).
    – dav
    Commented Sep 29, 2015 at 20:16
  • 1
    Oh, and the other thing is: Please don't use pie charts for that many data points, and please drop the 3D bezel effect. Why? Read Save the Pies for Dessert
    – teylyn
    Commented Sep 29, 2015 at 23:43
  • @teylyn - yeah, tell it to my boss. Realistically, we usually only have 4-6 elements, not the 15 I've set up my template for, but she want pie and she wants it shiny and curvy like all the rest... :/
    – FreeMan
    Commented Sep 30, 2015 at 12:05
  • @dav - I've seen suggestions on how to do that, however, I need an automated solution to create charts for 20 reports & the number of rows will change for each one. If you have a suggestion that will make nice charts without extra rows showing up as zero values (i.e. handle one chart source with different numbers of rows), please put it in an answer! I'll work that into my VBA solution, give you credit, and maybe even buy you a cup o' coffee!
    – FreeMan
    Commented Sep 30, 2015 at 15:12

1 Answer 1

1

To accomplish this without the use of Pivot Charts, all you need is a dynamic Named Range and a simple (if grossly formatted) chart.

1) Create a defined Name using this formula: Series_A=OFFSET(Sheet!B1,0,0,COUNT(A:A),1), where - Sheet1!B1 refers to your first count cell in your pivot table (or any other reference, if your comfortable with OFFSET) - Count(A:A) is your column with row labels

2) Create your chart(s) with the series=Series_A (and any others you need).

As you update your pivot tables, the defined names will reference the expanding/contracting values in your row labels column and only return the values in your count column. No 0's, #N/A's, or helper columns required.

1
  • Thanks dav! I got sidetracked for a while, but once I got back to this, that worked perfectly! I had issues getting Excel to accept the named range as the series source, then I discovered that (since the range was on another sheet) that SheetName!NamedRange would do the trick. I thought NamedRange would be sufficient, since it was defined with a Workbook scope, but Excel wouldn't take it. Greatly appreciated. Since I couldn't get a couple of singles crammed into the USB port, here's your cup o'Joe!
    – FreeMan
    Commented Oct 19, 2015 at 14:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .