2013-07-15 42 views
1

有點背景; 我必須在上週的星期一爲我的工作運行每週報告,但是我需要整理材料,我做了一個數據透視表,我必須爲多個工作表執行此操作。不過,我決定創建一個宏來完成這個冗餘任務。現在創建它我似乎得到這個錯誤消息「無效的過程或參數」。我不能讓它在我的新工作表打開,叔他是我的代碼>>VBA;無效的程序或參數錯誤

Sub weekmaster() 
' 
' weekmaster Macro 
' Macro for the week 
' 
' Keyboard Shortcut: Ctrl+t 
' 
    Cells.Select 
    Sheets.Add 
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ 
     "weekmaster!R1C1:R1048576C62", Version:=xlPivotTableVersion12). _ 
     CreatePivotTable TableDestination:="Sheet9!R3C1", TableName:="PivotTable1" _ 
     , DefaultVersion:=xlPivotTableVersion12 
    Sheets("Sheet9").Select 
    Cells(3, 1).Select 
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Order ID") 
     .Orientation = xlRowField 
     .Position = 1 
    End With 
+0

你如果TableDestination無效,可能會發生該錯誤。 (即不存在具有該名稱的表格)。 – Stepan1010

回答

0

,如果你跑的比宏觀更一旦你會得到一個錯誤,或者如果Sheet9已經存在(因爲宏試圖使用相同的名稱在同一張紙上創建相同的數據透視表)。如果我假設你在每次你去你的數據表和運行宏時產生的數據透視表在的工作表,您可以用下面的代碼更新:

Dim myRange as Range 
dim myNewSheet as Worksheet 

' Stores all continuous data on the sheet in the myRange variable 
Set myRange = Range("A1").CurrentRegion 

' Adds a new sheet and stores it in the myNewSheet variable 
Set myNewSheet = Sheets.Add 

' Use the variables to create the new pivot table 
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ 
    myRange, Version:=xlPivotTableVersion12).CreatePivotTable _ 
    TableDestination:=myNewSheet.Cells(3, 1), DefaultVersion _ 
    :=xlPivotTableVersion12 

' Select your Order ID field 
With myNewSheet.PivotTables(1).PivotFields("Order ID") 
    .Orientation = xlRowField 
    .Position = 1 
End With 
1

看來你缺少一個參數。 CreatePivotTable採用下列參數:

expression.CreatePivotTable(TableDestination,表名,READDATA,DefaultVersion)

  1. TableDestination Variant類型,必需在數據透視表的目標範圍(在該範圍的左上角細胞工作表所在的數據透視表報告將被放置)。目標範圍必須位於包含由expression指定的PivotCache對象的工作簿中的工作表上。
  2. TableName可選Variant新的數據透視表報告的名稱。
  3. ReadData可選Variant正確創建包含外部數據庫中所有記錄的數據透視表緩存;這個緩存可能非常大。 False,以便在實際讀取數據之前將某些字段設置爲基於服務器的頁面字段。
  4. DefaultVersion可選Variant數據透視表的默認版本。

隨後,您可能需要在TableName和DefaultVersion之間添加'true'。

乾杯,LC

相關問題