简体   繁体   中英

Creating a pivot table in VBA

I have an xlsm file with some VBA code in it. I basically have a folder with a lot of csv files in it. When I execute the VBA code in the xlsm file, it needs to loop through each csv file, select the data in it and then create a pivot table from the data and export the sheet with the pivot table into a fresh workbook.

It needs to be dynamic in the sense that the amount of data (rows) is not the same in each csv file, so it first needs to select the block of data and then create the pivotcaches etc.

My code fails when trying to create the pivot table. See below for the relevant code. This code kicks off the job to loop through each csv:

Sub RunBatch()

Dim Filename, Pathname As String
Dim wb As Workbook

Pathname = "\\troy\Anfield\Product & Risk Management\Muhammad\2014\PnL Attribution Reports\20140822\"
Filename = Dir(Pathname & "*.csv")

Do While Filename <> ""
    Set wb = Workbooks.Open(Pathname & Filename)
    CreatePivotTableSummary wb
    wb.Close SaveChanges:=True
    Filename = Dir()
Loop

End Sub

This code is the one that actually creates the pivot:

Sub CreatePivotTableSummary(wb As Workbook)

Dim WorkbookName As String
WorkbookName = wb.Name

wb.Activate

Set DataRange = Range(Selection, Selection.End(xlToRight))
Set DataRange = Range(Selection, Selection.End(xlDown))
Set OutputRange = Sheet2.Range("A3")

Sheets.Add

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=DataRange, Version:=xlPivotTableVersion14).CreatePivotTable TableDestination:=OutputRange, TableName:=WorkbookName, DefaultVersion:=xlPivotTableVersion14

Sheet2.Select

With wb.ActiveSheet.PivotTables(WorkbookName)
    .PivotFields("Portfolio").Orientation = xlRowField
    .PivotFields("Portfolio").Position = 1

    .PivotFields("TradePortfolio").Orientation = xlRowField
    .PivotFields("TradePortfolio").Position = 2

    .PivotFields("InstrType").Orientation = xlRowField
    .PivotFields("InstrType").Position = 3

    .AddDataField ActiveSheet.PivotTables(WorkbookName).PivotFields("TPL"), "Sum of TPL", xlSum
    .AddDataField ActiveSheet.PivotTables(WorkbookName).PivotFields("ThTPL Deco"), "Sum of ThTPL Deco", xlSum
    .AddDataField ActiveSheet.PivotTables(WorkbookName).PivotFields("Corr Attr"), "Sum of Corr Attr", xlSum
    .AddDataField ActiveSheet.PivotTables(WorkbookName).PivotFields("Cr Attr"), "Sum of Cr Attr", xlSum
    .AddDataField ActiveSheet.PivotTables(WorkbookName).PivotFields("Cross Effec"), "Sum of Cross Effec", xlSum
    .AddDataField ActiveSheet.PivotTables(WorkbookName).PivotFields("FX Attr"), "Sum of FX Attr", xlSum
    .AddDataField ActiveSheet.PivotTables(WorkbookName).PivotFields("Infl Attr"), "Sum of Infl Attr", xlSum
    .AddDataField ActiveSheet.PivotTables(WorkbookName).PivotFields("IR Attr"), "Sum of IR Attr", xlSum
    .AddDataField ActiveSheet.PivotTables(WorkbookName).PivotFields("Price Attr"), "Sum of Price Attr", xlSum
    .AddDataField ActiveSheet.PivotTables(WorkbookName).PivotFields("Residual"), "Sum of Residual", xlSum
    .AddDataField ActiveSheet.PivotTables(WorkbookName).PivotFields("Time Attr"), "Sum of Time Attr", xlSum
    .AddDataField ActiveSheet.PivotTables(WorkbookName).PivotFields("Trd Attr"), "Sum of Trd Attr", xlSum
    .AddDataField ActiveSheet.PivotTables(WorkbookName).PivotFields("Vol Attr"), "Sum of Vol Attr", xlSum
    .AddDataField ActiveSheet.PivotTables(WorkbookName).PivotFields("YC Attr"), "Sum of YC Attr", xlSum

    .PivotFields("Sum of TPL").NumberFormat = "#,##0"
    .PivotFields("Sum of ThTPL Deco").NumberFormat = "#,##0"
    .PivotFields("Sum of Corr Attr").NumberFormat = "#,##0"
    .PivotFields("Sum of Cr Attr").NumberFormat = "#,##0"
    .PivotFields("Sum of Cross Effec").NumberFormat = "#,##0"
    .PivotFields("Sum of FX Attr").NumberFormat = "#,##0"
    .PivotFields("Sum of Infl Attr").NumberFormat = "#,##0"
    .PivotFields("Sum of IR Attr").NumberFormat = "#,##0"
    .PivotFields("Sum of Price Attr").NumberFormat = "#,##0"
    .PivotFields("Sum of Residual").NumberFormat = "#,##0"
    .PivotFields("Sum of Time Attr").NumberFormat = "#,##0"
    .PivotFields("Sum of Trd Attr").NumberFormat = "#,##0"
    .PivotFields("Sum of Vol Attr").NumberFormat = "#,##0"
    .PivotFields("Sum of YC Attr").NumberFormat = "#,##0"

    .RowAxisLayout xlTabularRow
    .TableStyle2 = "PivotStyleMedium2"

    .PivotFields("InstrType").PivotFilters.Add Type:=xlValueDoesNotEqual, DataField:=ActiveSheet.PivotTables(WorkbookName).PivotFields("Sum of TPL"), Value1:=0

End With

End Sub

It fails at this point:

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=DataRange, Version:=xlPivotTableVersion14).CreatePivotTable TableDestination:=OutputRange, TableName:=WorkbookName, DefaultVersion:=xlPivotTableVersion14

with the error:

Invalid procedure call or argument

Please can someone assist?

Thank you

Sometimes, we get this error when any data range does not have a column Header. Check whether first row of your data range has some header for all columns.

Pivot table creation fails, if it could not find a column header.

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