得到另一個Excel VBA宏按鈕問題。運行時錯誤-2147024809(80070057) - 數據透視表字段名稱無效
我想做一個按鈕來刷新工作表上的所有樞軸(有四個)。我以前去過StackOverflow,並且收到了一個運行良好的解決方案。我把我之前提供並更新其新的工作簿的代碼,但它的功能.ChangePivotCache PvtCache如下失敗:
Sub Button1_Click()
Dim PvtTbl As PivotTable
Dim PvtCache As PivotCache
Dim PvtDataRng As Range
Set PvtDataRng = Worksheets("OTR_Promo_List_ADV_2017").Range("B2:AU500")
Set PvtCache = ActiveWorkbook.PivotCaches.Add(xlDatabase, PvtDataRng)
Set PvtTbl = Worksheets("Desired_Distribution").PivotTables("PivotTable1")
For Each PvtTbl In Worksheets("Desired_Distribution").PivotTables
Set PvtCache = ActiveWorkbook.PivotCaches.Add(xlDatabase, PvtDataRng)
With PvtTbl
.ChangePivotCache PvtCache
.RefreshTable
End With
Set PvtCache = Nothing
Next PvtTbl
End Sub
唯一的區別是,該工作簿改變,現在我想刷新4個繞代替2 - 這可能是問題嗎?
在屏幕截圖下方,您可以看到工作表。 「OTR_Promo_List_ADV-2017」是數據庫。透視表具有默認的命名,我沒有更改名稱(即PivotTable1,PivotTable2 ...)
謝謝!
我記得這個,但我的代碼有點不同。首先,你不需要'For Each PvtTbl ...'循環之前的兩行'Set PvtCache'和'Set PvtTbl'。第二,如果你的Range不是動態的(Range(「B2:AU500」是靜態的),爲什麼你需要運行這個代碼? –
我不知道,我簡直就是複製和粘貼我提供的任何東西...... – Kevbo