VBA - Programming Charts

VBA - Programming Charts

Using VBA, we will be able to do generate Charts based on certain criteria. Let us take a look at it with an example.
Step 1 : First Enter the data against which the graph has to be generated.
Graph in VBA
Step 2 : Let us create 3 buttons one to generate Bar graph, pie Chart, Column Chart.
Graph in VBA
Step 3 : Now let us develop a Macro to generate each one of these type of charts
' Procedure to Generate Pie Chart
Private Sub fn_generate_pie_graph_Click()
    Dim cht As ChartObject
    For Each cht In Worksheets(1).ChartObjects
        cht.Chart.Type = xlPie
    Next cht
End Sub


' Procedure to Generate Bar Graph
Private Sub fn_Generate_Bar_Graph_Click()
    Dim cht As ChartObject
    For Each cht In Worksheets(1).ChartObjects
        cht.Chart.Type = xlBar
    Next cht
End Sub

' Procedure to Generate Column Graph
Private Sub fn_generate_column_graph_Click()
    Dim cht As ChartObject
    For Each cht In Worksheets(1).ChartObjects
        cht.Chart.Type = xlColumn
    Next cht
End Sub
Step 4 : Upon clicking on the corresponding button, that chart is created. In the below output we have clicked on generate Pie Chart button.
Graph in VBA