Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting data from pivot table vba

I have a pivot table to aggregate "coverage" on "part" only for accepted parts.

enter image description here

I want then to extract the "sum of coverage" to another sheet. I wrote the following macro:

Sub Pull_data()
'Update the pivot table
Sheets("Pivot").PivotTables("PivotTable2").PivotCache.Refresh
'clear all filters
Sheets("Pivot").PivotTables("PivotTable2").PivotFields("Accepted").ClearAllFilters
'filters only accepted items
Sheets("Pivot").PivotTables("PivotTable2").PivotFields("Accepted").CurrentPage = "YES"
'get the last row of the pivot table
Set PT = Sheets("Pivot").PivotTables("PivotTable2")
With PT.TableRange1
    lngLastRow = .rows(.rows.Count).Row
End With
For i = 4 To lngLastRow
    'copy the coverage to destination sheet
    NEWi = i + 10
    Sheets("Destination").Range("G" & NEWi) = PivotTable.GetPivotData(data_field, Range("I" & i), “Coverage”)
Next i
End Sub

I get a run time error '424', object required on

Sheets("Destination").Range("G" & NEWi) = PivotTable.GetPivotData(data_field, Range("I" & i), “Coverage”)

Which would be the proper way to write that line?

like image 903
L.Dutch Avatar asked Feb 17 '17 13:02

L.Dutch


People also ask

How do I use a pivot table in VBA?

But in VBA, we have to create. For this, define the variable a PivotCache. Step 3: To determine the pivot data range define the variable as a range. Step 4: To insert a pivot table, we need a separate sheet to add worksheets for the pivot table to declare the variable as a worksheet.


2 Answers

This should be :

Sheets("Destination").Range("G" & i + 10).Value = _
    pT.GetPivotData("Sum of coverage", "Part", Range("I" & i).Value).Value

Because pT.GetPivotData returns a Range!

Cleaned code :

Sub Pull_data()
    Dim pT As PivotTable
    Set pT = Sheets("Pivot").PivotTables("PivotTable2")

    With pT
        '''Update the pivot table
        .PivotCache.Refresh
        '''clear all filters
        .PivotFields("Accepted").ClearAllFilters
        '''filters only accepted items
        .PivotFields("Accepted").CurrentPage = "YES"
        '''get the last row of the pivot table
        With .TableRange1
            lngLastRow = .Rows(.Rows.Count).Row
            For i = .Cells(2, 1).Row To lngLastRow
                Debug.Print "i=" & i & "|" & Sheets("Pivot").Range("I" & i).Value
                '''copy the coverage to destination sheet
                Sheets("Destination").Range("G" & i + 10).Value = _
                    pT.GetPivotData("Sum of coverage", "Part", Sheets("Pivot").Range("I" & i).Value).Value
            Next i
        End With '.TableRange1
    End With 'pT
End Sub
like image 155
R3uK Avatar answered Oct 22 '22 21:10

R3uK


You could try copying the entire Column from your PivotTable after it's filtered to your needs, with TableRange2 , use the Resize to a single column, and then Copy and PasteSpecial xlValues to the destination worksheet.

If the code below takes the wrong column, you can also use the Offset(0,1) to get the right one.

With PT
    .TableRange2.Resize(.TableRange2.Rows.Count, 1).Copy
    Worksheets("Destination").Range("G14").PasteSpecial xlValues '<-- start Pasting from Row 14
End With

Note: if the code above takes the column to the left, try the code line below:

.TableRange2.Resize(.TableRange2.Rows.Count, 1).Offset(, 1).Copy
like image 2
Shai Rado Avatar answered Oct 22 '22 19:10

Shai Rado