簡體   English   中英

在VBA中創建數據透視表

[英]Creating a pivot table in VBA

我有一個xlsm文件,其中包含一些VBA代碼。 我基本上有一個包含很多csv文件的文件夾。 當我在xlsm文件中執行VBA代碼時,它需要遍歷每個csv文件,選擇其中的數據,然后從數據中創建數據透視表,並將包含數據透視表的工作表導出到新的工作簿中。

從每個csv文件中的數據量(行)不同的角度來看,它必須是動態的,因此它首先需要選擇數據塊,然后創建數據透視緩存等。

嘗試創建數據透視表時,我的代碼失敗。 有關相關代碼,請參見下文。 這段代碼啟動了要遍歷每個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

這段代碼實際上是創建樞軸的代碼:

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

此時失敗:

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

與錯誤:

無效的過程調用或參數

有人可以幫忙嗎?

謝謝

有時,當任何數據范圍都沒有列標題時,就會出現此錯誤。 檢查數據范圍的第一行所有列是否都有某些標題。

如果數據透視表找不到列標題,則創建失敗。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM