The Vba Coding
The Vba Coding
Page 2 of 12
Inserting A Chart
Method 1:
Sub CreateChart()
'PURPOSE: Create a chart (chart
dimensions are not required)
Dim rng As Range
Dim cht As Object
'Your data range for the chart
Set rng = ActiveSheet.Range
("A24:M27")
'Create a chart
Set cht =
ActiveSheet.Shapes.AddChart2
'Give chart some data
cht.Chart.SetSourceData Source:=rng
'Determine the chart type
cht.Chart.ChartType =
xlXYScatterLines
Free Webinars
End Sub
http://www.thespreadsheetguru.com/blog/2015/3/1/the-vba-coding-guide-for-excel-charts-g... 30/09/16
The VBA Coding Guide For Excel Charts & Graphs The Spreadsheet Guru
Page 3 of 12
Method 2:
Sub CreateChart()
'PURPOSE: Create a chart (chart
dimensions are required)
Dim rng As Range
Dim cht As ChartObject
'Your data range for the chart
Set rng = ActiveSheet.Range
("A24:M27")
'Create a chart
Set cht =
ActiveSheet.ChartObjects.Add( _
Left:=ActiveCell.Left, _
Width:=450, _
Top:=ActiveCell.Top, _
Height:=250)
'Give chart some data
cht.Chart.SetSourceData Source:=rng
'Determine the chart type
cht.Chart.ChartType =
xlXYScatterLines
End Sub
Sub LoopThroughCharts()
'PURPOSE: How to cycle through charts
and chart series
Dim cht As ChartObject
Dim ser As Series
'Loop Through all charts on ActiveSheet
For Each cht In
ActiveSheet.ChartObjects
Next cht
'Loop through all series in a chart
For Each ser In
grph.Chart.SeriesCollection
Next ser
http://www.thespreadsheetguru.com/blog/2015/3/1/the-vba-coding-guide-for-excel-charts-g... 30/09/16
The VBA Coding Guide For Excel Charts & Graphs The Spreadsheet Guru
Sub AddChartTitle()
'PURPOSE: Add a title to a specific
chart
Page 4 of 12
Follow @ChrisMacro
End Sub
Sub RepositionChartTitle()
'PURPOSE: Reposition a chart's title
Dim cht As ChartObject
Set cht = ActiveSheet.ChartObjects
("Chart 1")
'Reposition title
With cht.Chart.ChartTitle
.Left = 100
.Top = 50
End With
End Sub
http://www.thespreadsheetguru.com/blog/2015/3/1/the-vba-coding-guide-for-excel-charts-g... 30/09/16
The VBA Coding Guide For Excel Charts & Graphs The Spreadsheet Guru
Page 5 of 12
Sub InsertChartLegend()
Dim cht As Chart
Set cht = ActiveSheet.ChartObjects
("Chart 1").Chart
'Add Legend to the Right
cht.SetElement
(msoElementLegendRight)
'Add Legend to the Left
cht.SetElement (msoElementLegendLeft)
'Add Legend to the Bottom
cht.SetElement
(msoElementLegendBottom)
'Add Legend to the Top
cht.SetElement (msoElementLegendTop)
'Add Overlaying Legend to the Left
cht.SetElement
(msoElementLegendLeftOverlay)
'Add Overlaying Legend to the Right
cht.SetElement
(msoElementLegendRightOverlay)
End Sub
Sub DimensionChartLegend()
Dim lgd As Legend
Set lgd = ActiveSheet.ChartObjects
("Chart 1").Chart.Legend
lgd.Left = 240.23
lgd.Top = 6.962
lgd.Width = 103.769
lgd.Height = 25.165
End Sub
http://www.thespreadsheetguru.com/blog/2015/3/1/the-vba-coding-guide-for-excel-charts-g... 30/09/16
The VBA Coding Guide For Excel Charts & Graphs The Spreadsheet Guru
Page 6 of 12
Sub AddStuffToChart()
Dim cht As Chart
Set cht = ActiveSheet.ChartObjects
("Chart 1").Chart
'Add X-axis
cht.HasAxis(xlCategory, xlPrimary) =
True '[Method #1]
cht.SetElement
(msoElementPrimaryCategoryAxisShow)
'[Method #2]
'Add X-axis title
cht.Axes(xlCategory,
xlPrimary).HasTitle = True '[Method #1]
cht.SetElement
(msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
'[Method #2]
'Add y-axis
cht.HasAxis(xlValue, xlPrimary) =
True '[Method #1]
cht.SetElement
(msoElementPrimaryValueAxisShow)
'[Method #2]
'Add y-axis title
cht.Axes(xlValue, xlPrimary).HasTitle
= True '[Method #1]
cht.SetElement
(msoElementPrimaryValueAxisTitleAdjacentToAxis)
'[Method #2]
'Add Data Labels (Centered)
cht.SetElement
(msoElementDataLabelCenter)
'Add Major Gridlines
cht.SetElement
(msoElementPrimaryValueGridLinesMajor)
'Add Linear Trend Line
cht.SeriesCollection
(1).Trendlines.Add Type:=xlLinear
End Sub
http://www.thespreadsheetguru.com/blog/2015/3/1/the-vba-coding-guide-for-excel-charts-g... 30/09/16
The VBA Coding Guide For Excel Charts & Graphs The Spreadsheet Guru
Page 7 of 12
Sub ChangeChartFormatting()
Dim cht As Chart
Set cht = ActiveSheet.ChartObjects
("Chart 1").Chart
'Adjust y-axis Scale
cht.Axes(xlValue).MinimumScale = 40
cht.Axes(xlValue).MaximumScale = 100
'Adjust x-axis Scale
cht.Axes(xlCategory).MinimumScale = 1
cht.Axes(xlCategory).MaximumScale =
10
'Adjust Bar Gap
cht.ChartGroups(1).GapWidth = 60
'Format Font Size
cht.ChartArea.Format.TextFrame2.TextRange.Font.Size
= 12
'Format Font Type
cht.ChartArea.Format.TextFrame2.TextRange.Font.Name
= "Arial"
'Make Font Bold
cht.ChartArea.Format.TextFrame2.TextRange.Font.Bold
= msoTrue
'Make Font Italicized
cht.ChartArea.Format.TextFrame2.TextRange.Font.Italic
= msoTrue
End Sub
Sub RemoveChartFormatting()
Dim cht As Chart
Set cht = ActiveSheet.ChartObjects
("Chart 1").Chart
http://www.thespreadsheetguru.com/blog/2015/3/1/the-vba-coding-guide-for-excel-charts-g... 30/09/16
The VBA Coding Guide For Excel Charts & Graphs The Spreadsheet Guru
Page 8 of 12
Sub ChangeChartColors()
Dim cht As Chart
Set cht = ActiveSheet.ChartObjects
("Chart 1").Chart
'Change first bar chart series fill
color
cht.SeriesCollection
(1).Format.Fill.ForeColor.RGB = RGB(91,
155, 213)
'Change X-axis label color
cht.Axes
(xlCategory).TickLabels.Font.Color =
RGB(91, 155, 213)
'Change Y-axis label color
cht.Axes
(xlValue).TickLabels.Font.Color = RGB
http://www.thespreadsheetguru.com/blog/2015/3/1/the-vba-coding-guide-for-excel-charts-g... 30/09/16
The VBA Coding Guide For Excel Charts & Graphs The Spreadsheet Guru
Page 9 of 12
http://www.thespreadsheetguru.com/blog/2015/3/1/the-vba-coding-guide-for-excel-charts-g... 30/09/16