Excel for data visualization

Excel is a powerful tool for analyzing and visualizing data, and it comes with a variety of built-in charting options that allow you to create a wide range of charts and graphs. However, sometimes you may want to create more customized visualizations that are not possible with the built-in options. In these cases, you can use Visual Basic for Applications (VBA) to create custom charts and graphs or to modify existing charts in Excel.

VBA is a programming language that is built into Excel and can be used to automate and customize various tasks in the program. It is easy to learn, even if you have no programming experience, and it allows you to create more advanced visualizations that are not possible with the built-in charting options.

To use VBA to create custom charts and graphs, you first need to open the Visual Basic editor in Excel. This can be done by going to the Developer tab in the ribbon and clicking on the Visual Basic button, or by pressing Alt + F11.

Once the Visual Basic editor is open, you can create a new module by clicking on the Insert tab and selecting Module. This will open a new code window where you can write your VBA code.

To create a custom chart or graph, you can use the ChartObjects collection and the Add method to add a new chart to a worksheet. You can then use the Chart object and its various properties and methods to customize the appearance and behavior of the chart.

For example, the following code creates a new column chart on a new worksheet and sets the chart title, x-axis title, and y-axis title:


Sub CreateColumnChart()

Dim ws As Worksheet
Set ws = Worksheets.Add

Dim cht As ChartObject
Set cht = ws.ChartObjects.Add(Left:=100, Top:=100, Width:=600, Height:=400)

With cht.Chart
    .ChartType = xlColumnClustered
    .SetSourceData Source:=Range("A1:B5")
    .HasTitle = True
    .ChartTitle.Text = "My Chart"
    .Axes(xlCategory, xlPrimary).HasTitle = True
    .Axes(xlCategory, xlPrimary).AxisTitle.Text = "Category"
    .Axes(xlValue, xlPrimary).HasTitle = True
    .Axes(xlValue, xlPrimary).AxisTitle.Text = "Value"
End With

End Sub

To modify an existing chart, you can use the ChartObjects collection and the Item method to reference the chart you want to modify. You can then use the Chart object and its various properties and methods to make your changes.

For example, the following code changes the chart type of an existing chart on the active worksheet to a line chart:


Sub ChangeChartType()

Dim cht As ChartObject
Set cht = ActiveSheet.ChartObjects(1)

With cht.Chart
    .ChartType = xlLine
End With

End Sub

Using VBA to create custom charts and graphs in Excel can be a powerful way to enhance your data visualization capabilities. With a little bit of programming knowledge, you can create highly customized visualizations that are not possible with the built-in charting options.

Another Example

Imagine you have a worksheet with data on the number of products sold by each salesperson in your company over the past year. You want to create a bar chart that shows the total number of products sold by each salesperson, but you also want to include an additional data series that shows the percentage of total sales each salesperson contributed.

To do this, you can use VBA to create a stacked bar chart with two data series. The first series will show the number of products sold by each salesperson, and the second series will show the percentage of total sales each salesperson contributed.

First, open the Visual Basic editor in Excel and create a new module. Then, use the following code to create the stacked bar chart:


Sub CreateStackedBarChart()

Dim ws As Worksheet
Set ws = Worksheets("Sheet1")

Dim cht As ChartObject
Set cht = ws.ChartObjects.Add(Left:=100, Top:=100, Width:=600, Height:=400)

With cht.Chart
    .ChartType = xlBarStacked
    .SetSourceData Source:=Range("A1:C5")
    .HasTitle = True
    .ChartTitle.Text = "Total Sales by Salesperson"
    .Axes(xlCategory, xlPrimary).HasTitle = True
    .Axes(xlCategory, xlPrimary).AxisTitle.Text = "Salesperson"
    .Axes(xlValue, xlPrimary).HasTitle = True
    .Axes(xlValue, xlPrimary).AxisTitle.Text = "Number of Products Sold"
End With

End Sub

This code creates a new stacked bar chart on a new worksheet and sets the chart title, x-axis title, and y-axis title. The data for the chart is taken from the range A1:C5, which should contain the salesperson names in column A, the number of products sold in column B, and the percentage of total sales in column C.

To add the data series for the percentage of total sales, you can use the SeriesCollection object and the Add method to add a new data series to the chart. You can then use the Series object and its various properties and methods to customize the appearance and behavior of the data series.

For example, the following code adds the data series for the percentage of total sales and formats it with a different color and pattern:


Sub AddPercentageDataSeries()

Dim ws As Worksheet
Set ws = Worksheets("Sheet1")

Dim cht As ChartObject
Set cht = ws.ChartObjects(1)

With cht.Chart.SeriesCollection.NewSeries
    .Name = "Percentage of Total Sales"
    .Values = ws.Range("C1:C5")
    .Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
    .Format.Line.DashStyle = msoLineLongDashDot
End With

End Sub


This code adds a new data series to the stacked bar chart, using the data from range C1:C5 as the values for the series. It also formats the data series with a red fill color and a long dash-dot line pattern.

Post a Comment

Previous Post Next Post