Beruflich Dokumente
Kultur Dokumente
CHECK OUT the recently launched My First Add-in templates and start creating your own add-ins today! ×
Search
Just Launched!
What if you have 20 graphs on a spreadsheet and they all need to have their legends in the exact same spot? What
if you create a bunch of charts and your manager needs the series colors changed at the last minute? Do you want to
do this all manually?
Below will be your cheat sheet for manipulating Excel charts & graphs with VBA code. Please let me know via the
comments section if there are areas missing from this guide so I can expand on them. Enjoy!
Inserting A Chart
Method 1:
Sub CreateChart()
'PURPOSE: Create a chart (chart dimensions are not required)
'Create a chart
Set cht = ActiveSheet.Shapes.AddChart2
End Sub
Latest VBA Code Vault Posts
Sub CreateChart()
'PURPOSE: Create a chart (chart dimensions are required)
https://www.thespreadsheetguru.com/blog/2015/3/1/the-vba-coding-guide-for-excel-charts-graph 1/8
7/6/2019 The VBA Coding Guide For Excel Charts & Graphs — The Spreadsheet Guru
Dim cht As ChartObject
VBA To Only Change
'Your data range for the chart
Set rng = ActiveSheet.Range("A24:M27")
Selection's Border Color
4 MONTHS AGO
'Create a chart
Set cht = ActiveSheet.ChartObjects.Add( _
Left:=ActiveCell.Left, _
Width:=450, _
Remove Page Breaks From All
Top:=ActiveCell.Top, _ Open Spreadsheets
Height:=250)
5 MONTHS AGO
'Give chart some data
cht.Chart.SetSourceData Source:=rng
Sub LoopThroughCharts()
'PURPOSE: How to cycle through charts and chart series
Next ser
Next cht
The VBA Guide To Excel Pivot
End Sub Tables
5 YEARS AGO
End Sub
Follow @ChrisMacro
Sub RepositionChartTitle()
'PURPOSE: Reposition a chart's title The Spreadsh…
2.2K likes
Dim cht As ChartObject
End Sub
Sub InsertChartLegend()
https://www.thespreadsheetguru.com/blog/2015/3/1/the-vba-coding-guide-for-excel-charts-graph 2/8
7/6/2019 The VBA Coding Guide For Excel Charts & Graphs — The Spreadsheet Guru
'Add Legend to the Top
cht.SetElement (msoElementLegendTop)
End Sub
Sub DimensionChartLegend()
lgd.Left = 240.23
lgd.Top = 6.962
lgd.Width = 103.769
lgd.Height = 25.165
End Sub
Sub AddStuffToChart()
'Add X-axis
cht.HasAxis(xlCategory, xlPrimary) = True '[Method #1]
cht.SetElement (msoElementPrimaryCategoryAxisShow) '[Method #2]
'Add y-axis
cht.HasAxis(xlValue, xlPrimary) = True '[Method #1]
cht.SetElement (msoElementPrimaryValueAxisShow) '[Method #2]
End Sub
Sub ChangeChartFormatting()
End Sub
Sub RemoveChartFormatting()
https://www.thespreadsheetguru.com/blog/2015/3/1/the-vba-coding-guide-for-excel-charts-graph 3/8
7/6/2019 The VBA Coding Guide For Excel Charts & Graphs — The Spreadsheet Guru
Dim cht As Chart
'Remove Gridlines
cht.Axes(xlValue).MajorGridlines.Delete
cht.Axes(xlValue).MinorGridlines.Delete
'Remove X-axis
cht.Axes(xlCategory).Delete
'Remove Y-axis
cht.Axes(xlValue).Delete
'Remove Legend
cht.Legend.Delete
'Remove Title
cht.ChartTitle.Delete
End Sub
Sub ChangeChartColors()
End Sub
https://www.thespreadsheetguru.com/blog/2015/3/1/the-vba-coding-guide-for-excel-charts-graph 4/8
7/6/2019 The VBA Coding Guide For Excel Charts & Graphs — The Spreadsheet Guru
Did you find this post helpful? Do you want to support this blog because you're just that awesome?! By sharing this
post on Facebook, Twitter, or Google+ you are not only providing others with great information, you are creating
exposure for The Spreadsheet Guru website. Growing this community is my number one goal as I have found
learning to increase exponentially when lots of people are contributing to the conversation. Learning is the whole
reason why this blog exists! If you want to spread the word just click on the Share button right below this paragraph
(next to the Like button -- I enjoy "likes" too!) or on the Floating Share Bar to the left and select your preferred social
medium. Thank you so much for reading and I hope I can continue to provide you with great content in the future!
Cheers!
REMOVE BLANK ROWS & COLUMNS WITH ... THE BEST WAY TO IMPROVE VBA MACRO ...
24 Comments TheSpreadsheetGuru
1 Login
LOG IN WITH
OR SIGN UP WITH DISQUS ?
Name
Thank you so much for all the information required for creating customized charts. I am quite new to VBA
and I learn most of this through blogs online and this is definitely a huge lesson for me! :)
The 'change your colors' section is exactly what I was looking for. But may I probe further and ask if there
is a way to set certain colors for series names in a chart? I was looking it at this option because I am
creating a dashboard from pivot tables which have a large number of products (series) and these would
repeat over in different graphs.
For i = 0 To LbSeries.ListCount - 1
SerieExiste = False
If LbSeries.Selected(i) = True Then
'vérifier ici si la série existe déjà sinon l'ajouter
For Each UneSerie In LeGraphique.Chart.SeriesCollection
If UneSerie.Name = LbSeries.List(i) Then
'MsgBox "déjà là"
SerieExiste = True
see more
△ ▽ • Reply • Share ›
'Add y-axis
cht.SetElement (msoElementPrimaryValueAxisShow)
R d J
https://www.thespreadsheetguru.com/blog/2015/3/1/the-vba-coding-guide-for-excel-charts-graph 6/8
7/6/2019 The VBA Coding Guide For Excel Charts & Graphs — The Spreadsheet Guru
Regards Jan.
△ ▽ • Reply • Share ›
Thanks
△ ▽ • Reply • Share ›
I am having a big problem here, how do I create a chart with multiple series where each series is a
unique array?
△ ▽ • Reply • Share ›
ALSO ON THESPREADSHEETGURU
How To Build A Modern-Looking VBA Userform How To Create A Filtering Search Box For Your
13 comments • 3 months ago Excel Data
Daniel Lamarche — Good taste with userforms can 1 comment • a year ago
Avatarmake the difference between users liking them or J M — Love the tutorial! Any way to add option
not so much...Thanks for good example! Avatarbuttons to change the DataRange from the active
sheet to other sheets in the workbook?
How To Determine Hex Color Codes For VBA How To Automatically Run Excel VBA Macros
Userforms Daily
2 comments • 2 months ago 7 comments • 2 months ago
Jon Peltier — You can just paste the decimal color Andrija — click on " Run whether or not user is
Avatarvalue, calculated from RGB(red, green, blue) into Avatarlogged on or not"..."MS Office applications will only
the Properties pane. VBA will convert it to hex … run under an active foreground user session. …
✉ Subscribe d Add Disqus to your siteAdd DisqusAdd 🔒 Disqus' Privacy PolicyPrivacy PolicyPrivacy
https://www.thespreadsheetguru.com/blog/2015/3/1/the-vba-coding-guide-for-excel-charts-graph 7/8
7/6/2019 The VBA Coding Guide For Excel Charts & Graphs — The Spreadsheet Guru
https://www.thespreadsheetguru.com/blog/2015/3/1/the-vba-coding-guide-for-excel-charts-graph 8/8