Macro to create a chart

keith2511

New Member
Joined
Dec 5, 2015
Messages
3
I need to create lots of pie charts for a dashboard and was hoping to record a macro that I could run once I have selected the data.

However the macro fails here:

Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+z
'
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlPie
ActiveChart.SetSourceData Source:=Range("'Trend Table'!$M$3:$M$4")
ActiveChart.ApplyChartTemplate ( _
"C:\Users\k\AppData\Roaming\Microsoft\Templates\Charts\Mini-pie.crtx" _
)
ActiveSheet.Shapes("Chart 7").Height = 93.5433070866
ActiveSheet.Shapes("Chart 7").Width = 96.3779527559
End Sub

I guess it's because Chart 7 already exists? Also the area I used to record the macro is hard coded in, even though I chose to use relative addresses.

Grateful for advice on how to get around this.

Thanks in advance.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi

Code:
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer$, nSize&) As Long


Sub Macro1()
Dim lpBuff As String * 25, ret&, UName$, ch As Shape
ret = GetUserName(lpBuff, 25)
Set ch = ActiveSheet.Shapes.AddChart
ch.Chart.ChartType = xlPie
ch.Chart.SetSourceData Source:=Range("'Trend Table'!" & Selection.Address)  ' selected cells
ch.Chart.ApplyChartTemplate ("C:\Users\" & Left(lpBuff, InStr(lpBuff, Chr(0)) - 1) & _
"\AppData\Roaming\Microsoft\Templates\Charts\minipie.crtx")
ch.Height = 94
ch.Width = 96
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,322
Members
452,635
Latest member
laura12345

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