Default chart types in VB

Weaver

Well-known Member
Joined
Sep 10, 2008
Messages
5,197
I wrote some code a while ago that runs a pivot table/pivot chart on a selected set of data, using

Code:
 charts.add
but this fails if the defaut chart type isn't compatible with the dataset. I'm trying to use a line graph, so is it possible to:

1 record the current defalut chart type
2 change it to what's required
3 draw the chart and
4 change the default back to what it was.

I found

expression.SetDefaultChart(FormatName)

in the help file but unless you're using a custom autoformat, the only other option is xlBuiltIn.

Any ideas?
 
Last edited:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Okay, so I got part 2

Code:
Application.SetDefaultChart FormatName:=xlLineMarkers

(obvious, really...)

but since other users will be using my code and they might be quite happy with their current default chart, I need to be able to detect it and record the value somewhere, so I can change it back when done.
 
Last edited:
Upvote 0
If you select a blank cell first, you can just create the chart, change it to whatever type you want, then use SetSourceData to assign the pivot table's tablerange to the chart.
 
Upvote 0
If you select a blank cell first, you can just create the chart, change it to whatever type you want, then use SetSourceData to assign the pivot table's tablerange to the chart.
There's always more than one way to skin the proverbial cat. Thanks for that piece of lateral thinking!
 
Upvote 0
Sometimes we just need a way to stop Excel being 'helpful'. ;)
 
Upvote 0
If anyone else reads this, you need to set the source to the datarange1 property of your pivot table, after setting the type to xlLine or similar.

For your blank cell, you can also use the cell where the page fields appear, so long as you haven't set any first, otherwise

Code:
 cells(rows.count,1).end(xlup).offset(1).select
will do
 
Upvote 0
I think you mean TableRange1 rather than Datarange1.
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

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