Automate the creation of graph from Pivot Table - VBA

Bryan123

New Member
Joined
May 23, 2019
Messages
41
Hi All,

I have created a VBA that automatically creates a pivot table. However, I would also like to automate the creation of a graph from the the pivot table. What I want to happen is to make a graph from the details without including "Row Labels" and "Grand Total". If there is a new entry for row, how do I capture it as well? Please help me.


Count of StatusColumn Labels
Row Labels> 5 DaysNAUncorrectedWithin 1 DayWithin 5 DaysGrand Total
Claims - Credit & Surety Claims 1 1
Claims - Long Tail 22
Claims - Short Tail 61 7
Claims - Workers Compensation - Claims11 3 5
Policy Support Services - Credit Control14 6 11
Policy Support Services - Elders 2 4 6
Policy Support Services - Financial Institution 235111
Policy Support Services - Motorcycle 4 4
Grand Total219419347
<colgroup><col width="305" style="width: 229pt; mso-width-source: userset; mso-width-alt: 9760;"> <col width="129" style="width: 97pt; mso-width-source: userset; mso-width-alt: 4128;"> <col width="27" style="width: 20pt; mso-width-source: userset; mso-width-alt: 864;"> <col width="96" style="width: 72pt; mso-width-source: userset; mso-width-alt: 3072;"> <col width="93" style="width: 70pt; mso-width-source: userset; mso-width-alt: 2976;"> <col width="101" style="width: 76pt; mso-width-source: userset; mso-width-alt: 3232;"> <col width="91" style="width: 68pt; mso-width-source: userset; mso-width-alt: 2912;"> <col width="65" style="width: 49pt; mso-width-source: userset; mso-width-alt: 2080;"> <tbody> </tbody>
 
When you say "generates a raw file", do you mean that the data is imported into your workbook? If so, there's no need to keep re-creating your pivottable and pivotchart. If you are adding new data to the exising one, after you do so simply adjust the range for the source data for your pivottable, and refresh. If you are replacing the existing data with a new set of data, first clear the existing one, then import the new data, then change/update the source data for the pivottable, and then refresh. In the latter case, you may also need to re-add your pivot fields for the report.


At this point, if you simply want my original macro to be amended as you've described, here it is. It will first delete any chart that already exists, and then it creates a new one. I've assumed that "Sheet1" will contain the chart. Also, the newly created chart will be named "Chart 1". Change these accordingly.


Code:
Sub CreatePivotChart()


    Dim destWorksheet As Worksheet
    Dim sourcePivotTable As pivotTable
    Dim sourceRange As Range
    Dim rowGrand As Long
    Dim colGrand As Long
    
    Const CHART_WORKSHEET As String = "Sheet1"
    Const CHART_NAME As String = "Chart 1"
    
    On Error Resume Next
    Application.DisplayAlerts = False
    ThisWorkbook.Worksheets(CHART_WORKSHEET).ChartObjects(CHART_NAME).Delete
    Application.DisplayAlerts = True
    On Error GoTo 0
    
    Set sourcePivotTable = ThisWorkbook.Worksheets("Pivot").PivotTables("PivotTable1")
    
    With sourcePivotTable
        rowGrand = IIf(.rowGrand, 1, 0)
        colGrand = IIf(.ColumnGrand, 1, 0)
        With .TableRange1
            Set sourceRange = .Resize(.Rows.Count - rowGrand, .Columns.Count - colGrand)
        End With
    End With
    
    Set destWorksheet = ThisWorkbook.Worksheets(CHART_WORKSHEET)
    
    With destWorksheet
        With .ChartObjects.Add(Left:=.Range("B2").Left, Top:=.Range("B2").Top, Width:=-1, Height:=-1)
            .Name = CHART_NAME
            With .Chart
                .ChartType = xlColumnClustered
                .SetSourceData sourceRange
            End With
        End With
    End With




End Sub
 
Last edited:
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

Forum statistics

Threads
1,215,008
Messages
6,122,672
Members
449,091
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top