Learn how to automate chart creation and customization using Excel VBA
Excel VBA provides powerful tools to create and customize charts programmatically. Whether you need to generate reports, visualize data trends, or create dashboards, VBA can automate these processes saving you countless hours of manual work.
In Excel VBA, we can write code to create charts and graphs easily based on spreadsheet data. Excel's charting engine is part of the Shape object and is also an object itself. We can create charts on their own sheets or embed them into existing worksheets. Chart sheets are Chart objects while embedded charts are part of the worksheet's shape collection.
Pie charts are excellent for showing proportions and percentages. Let's walk through creating a 3D pie chart with VBA.
Private Sub CommandButton1_Click()
'Create and position the chart
ActiveSheet.Shapes.AddChart.Select
ActiveSheet.Shapes(1).Top = 10
ActiveSheet.Shapes(1).Left = 10
'Set chart type and data source
ActiveChart.ChartType = xl3DPie
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("MyChart")
'Add and format chart title
ActiveChart.HasTitle = True
ActiveChart.ChartTitle.Text = "Sales Distribution"
ActiveChart.ChartTitle.Font.Size = 14
ActiveChart.ChartTitle.Font.Bold = True
'Format data labels
ActiveChart.ApplyDataLabels
ActiveChart.SeriesCollection(1).DataLabels.ShowPercentage = True
ActiveChart.SeriesCollection(1).DataLabels.ShowCategoryName = True
End Sub
To emphasize specific segments, you can "explode" the pie chart by separating slices. Add this line to your code:
ActiveChart.SeriesCollection(1).Explosion = 10
This will separate all slices equally. To explode just one slice, you would need to access individual data points.
Bar graphs (or column charts) are ideal for comparing values across categories. Here's how to create one with VBA:
Private Sub CommandButton2_Click()
'Create and position the chart
ActiveSheet.Shapes.AddChart.Select
ActiveSheet.Shapes(1).Top = 10
ActiveSheet.Shapes(1).Left = 10
'Set chart type and data source
ActiveChart.ChartType = xl3DColumn
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("MyChart")
'Add and format chart title
ActiveChart.HasTitle = True
ActiveChart.ChartTitle.Text = "Quarterly Sales"
'Format axes
ActiveChart.Axes(xlCategory).HasTitle = True
ActiveChart.Axes(xlCategory).AxisTitle.Text = "Products"
ActiveChart.Axes(xlValue).HasTitle = True
ActiveChart.Axes(xlValue).AxisTitle.Text = "Sales (USD)"
'Add data labels
ActiveChart.SeriesCollection(1).ApplyDataLabels
End Sub
Excel VBA supports numerous chart types through the ChartType property. Here's a comprehensive reference table:
Property | Chart Type | Best For |
---|---|---|
xlArea | Area Chart | Displaying magnitude changes over time |
xlBar | Bar Chart | Comparing values across categories |
xlColumn | Column Chart | Similar to bar but vertical orientation |
xlLine | Line Chart | Showing trends over time |
xlPie | Pie Chart | Displaying proportions/percentages |
xlXYScatter | Scatter Plot | Showing relationships between variables |
xl3DArea | 3D Area Chart | Area chart with depth perspective |
xl3DBar | 3D Bar Chart | Bar chart with depth perspective |
xl3DColumn | 3D Column Chart | Column chart with depth perspective |
xl3DLine | 3D Line Chart | Line chart with depth perspective |
xlBubble | Bubble Chart | Displaying three dimensions of data |
xlRadar | Radar Chart | Comparing multiple quantitative variables |
Line charts are perfect for showing trends over time. Here's how to create one:
Private Sub CreateLineChart()
Dim chrt As ChartObject
'Create chart object
Set chrt = ActiveSheet.ChartObjects.Add(Left:=100, Width:=375, Top:=75, Height:=225)
'Set chart properties
With chrt.Chart
.ChartType = xlLine
.SetSourceData Source:=Range("MyTimeSeriesData")
.HasTitle = True
.ChartTitle.Text = "Monthly Sales Trend"
'Format axes
.Axes(xlCategory).HasTitle = True
.Axes(xlCategory).AxisTitle.Text = "Months"
.Axes(xlValue).HasTitle = True
.Axes(xlValue).AxisTitle.Text = "Revenue"
'Format series
With .SeriesCollection(1)
.Border.Color = RGB(0, 112, 192)
.Border.Weight = xlMedium
.MarkerStyle = xlMarkerStyleCircle
.MarkerSize = 7
End With
End With
End Sub
Create charts with multiple chart types (e.g., column + line):
Sub CreateCombinationChart()
Dim cht As Chart
Set cht = Charts.Add
With cht
.ChartType = xlColumnClustered
.SetSourceData Source:=Range("A1:C6")
.SeriesCollection(2).ChartType = xlLine
.SeriesCollection(2).AxisGroup = 2
.HasTitle = True
.ChartTitle.Text = "Sales vs. Target"
End With
End Sub
Make charts automatically adjust to changing data ranges:
Sub DynamicRangeChart()
Dim LastRow As Long
Dim rng As Range
'Find last row with data
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
'Define dynamic range
Set rng = Range("A1:B" & LastRow)
'Create chart
With ActiveSheet.ChartObjects.Add(Left:=100, Width:=375, Top:=75, Height:=225).Chart
.ChartType = xlColumnClustered
.SetSourceData Source:=rng
End With
End Sub
Shapes.AddChart
or ChartObjects.Add
to create chartsChartType
property (xlPie, xlColumn, xlLine, etc.)SetSourceData
methodCreate a macro that generates a dashboard with:
Copyright ® 2008 Dr.Liew Voon Kiong . All rights reserved [Privacy Policy]
Contact: Facebook Page