對于我的資料透視表,我想從我的源表中為每個選定的標題(例如“A”、“B”、“C”等)創建一個資料欄位。我沒有為每個標題撰寫單獨的代碼,而是通過標題創建一個回圈,因為我有超過 10 個標題。
我嘗試的回圈代碼:
Dim h As Variant
Dim p As Long
Dim hdrs As Variant 'table headers of interest
hdrs = Array("A","B","C")
For Each h In hdrs
With PSheet.PivotTables(PTName).PivotFields(hdrs)
.Orientation = xlDataField
.Function = xlAverage
.NumberFormat = "0.00"
.Position = p 1
End With
Next h
我的代碼變數:
- PSheet:保存我的資料透視表的作業表(作業表變數)
- PTName:資料透視表的名稱(字串變數)
但是這段代碼沒有任何反應
uj5u.com熱心網友回復:
這有效:
Dim hdrs As Variant
Dim i As Long
hdrs = Array("A", "B", "C")
For Each Item In hdrs
PSheet.PivotTables(PTName).PivotFields (Item)
With PSheet.PivotTables(PTName).PivotFields(Item)
.Orientation = xlDataField
.Function = xlAverage
.NumberFormat = "0.00"
.Position = i 1
End With
Next Item
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/420724.html
標籤:
上一篇:比較具有多列的兩個檔案
