In Excel VBA 365, we can automate the creation of professional charts and graphs to visualize spreadsheet data. This powerful feature saves time and ensures consistency in your reports and dashboards.
Pie charts are excellent for showing proportional relationships. Here's how to create one with VBA:
The complete VBA code to create a 3D pie chart:
Private Sub CommandButton1_Click()
'Add 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
End Sub
To emphasize specific segments, use the exploded pie chart variant:
'Change this line in the previous code
ActiveChart.ChartType = xl3DPieExploded
Bar charts (or column charts) are ideal for comparing values across categories. Here's the VBA code to create one:
Private Sub CommandButton2_Click()
'Add 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 chart title
ActiveChart.HasTitle = True
ActiveChart.ChartTitle.Text = "Quarterly Sales Report"
'Format axes
ActiveChart.Axes(xlCategory).HasTitle = True
ActiveChart.Axes(xlCategory).AxisTitle.Text = "Products"
ActiveChart.Axes(xlValue).HasTitle = True
ActiveChart.Axes(xlValue).AxisTitle.Text = "Sales ($)"
End Sub
Excel VBA 365 supports numerous chart types. Here are the most commonly used ones:
Property | Chart Type | Best For |
---|---|---|
xlArea | Area Chart | Showing magnitude over time |
xlBar | Bar Chart | Comparing values across categories |
xlColumn | Column Chart | Vertical bar comparisons |
xlLine | Line Chart | Trends over time |
xl3DLine | 3D Line Chart | Multi-series trends |
xlPie | Pie Chart | Proportional relationships |
xlXYScatter | Scatter Plot | Correlation between variables |
xl3DArea | 3D Area Chart | Volume representation |
xl3DBar | 3D Bar Chart | Enhanced visual comparisons |
xl3DColumn | 3D Column Chart | Dramatic value comparisons |
For a complete list of ChartType properties, refer to the official Microsoft documentation.
Line charts are perfect for showing trends over time. Here's how to create one with VBA:
Private Sub CommandButton3_Click()
'Add 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 = xlLine
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("MyChart")
'Add chart title
ActiveChart.HasTitle = True
ActiveChart.ChartTitle.Text = "Monthly Sales Trend"
'Format line style
ActiveChart.SeriesCollection(1).Format.Line.Weight = 2.5
ActiveChart.SeriesCollection(1).Format.Line.ForeColor.RGB = RGB(0, 0, 255)
'Add data labels
ActiveChart.SeriesCollection(1).HasDataLabels = True
ActiveChart.SeriesCollection(1).DataLabels.NumberFormat = "$#,##0"
End Sub
Excel VBA allows extensive customization of your charts. Here are some useful techniques:
'Position the legend
ActiveChart.Legend.Position = xlLegendPositionBottom
'Format legend text
ActiveChart.Legend.Font.Size = 10
ActiveChart.Legend.Font.Bold = True
'Change series color
ActiveChart.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
'Apply gradient fill to chart area
With ActiveChart.ChartArea.Format.Fill
.ForeColor.RGB = RGB(255, 255, 255)
.TwoColorGradient msoGradientHorizontal, 1
End With
To clean up your worksheets, use this code to delete all charts:
'Delete all charts on Sheet1
Worksheets("Sheet1").ChartObjects.Delete
'Delete specific chart by name
Worksheets("Sheet1").ChartObjects("Chart 1").Delete
Shapes.AddChart
method to create charts programmaticallyChartType
propertySetSourceData
method with named rangesChartObjects.Delete
to remove charts from worksheets
Copyright ® 2020 Dr.Liew Voon Kiong . All rights reserved [Privacy Policy]
Contact: Facebook Page