简体   繁体   中英

Copy a Pivot Table with VBA

I'm programming an excel macro that is supposed to create tables and graphs in a new summary worksheet, and I'm having a problem in a part where the macro needs to filter a Pivot Table, and then copy/paste it in the summary worksheet. Sounds simple, but for a reason I get just a single cell with value 0 in the destination range. The filtering is done right, however. Here's the part of the code where this happens.

    Dim pt As PivotTable: Set pt = errprov.PivotTables("ErroresProveedor")
    On Error Resume Next
    pt.PivotFields("DESCRIPCIONESGLOBALES").ClearAllFilters
    pt.PivotFields("DESCRIPCIONESGLOBALES").CurrentPage =actual.Range("A2").Value
    pt.PivotFields("DATA DE").ClearAllFilters
    pt.PivotFields("DATA DE").CurrentPage = "SA"

    'The problem is here
    Dim rng As Range
    Set rng = errprov.Range("A7", Range("A7").End(xlToRight).End(xlDown))
    actual.Activate
    rng.Copy Range("A" & fila)

I get an error with the range property, probably because there's a Pivot Table there, but I have also tried selecting the table as an object with its own method, like I found in other posts, and I get the same result (Single cell with value 0).

Set rng = errprov.Range("A7", Range("A7").End(xlToRight).End(xlDown))

应该

Set rng = errprov.Range("A7:" & Range("A7").End(xlToRight).End(xlDown).Address)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM