2014-08-27 230 views
1

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

它需要在這個意義上的數據(行)的量不每個csv文件相同的動態的,因此它首先需要選擇數據塊,然後創建的PivotCaches等

嘗試創建數據透視表時,我的代碼失敗。請參閱下面的相關代碼。此代碼揭開序幕的工作,遍歷每個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 

與錯誤:

Invalid procedure call or argument

請有人協助?

謝謝

+0

調試時,你的OutputRange和WorkbookName變量會返回什麼? – Dave 2014-08-27 14:47:46

+0

我現在不在我的工作電腦前面,但他們應該返回什麼? – Fayyaadh 2014-08-27 15:23:06

+0

我如何檢查?它處於調試模式,當我將鼠標懸停在「DataRange」和「OutputRange」上時,什麼也不顯示。 – Fayyaadh 2014-08-28 07:03:58

回答

0

有時候,當任何數據範圍並沒有列標題,我們得到這個錯誤。 檢查數據範圍的第一行是否對所有列都有一些標題。

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

+0

所有列都有一個標題,所以不是這樣。我通過使用另一個堆棧溢出問題中發佈的語法來解決它。將編輯我的問題以包含可用的代碼。 – Fayyaadh 2014-08-31 13:43:56

+0

嗨Fayyaadh,我有同樣的問題。你是否還有解決它的問題的鏈接?非常感謝。 – xav 2015-01-22 12:39:40