跳到主要內容

如何更改數據透視表中的多個字段設置?

在工作表中創建數據透視表時,將字段拖到 價值觀 列出 數據透視表字段列表,您可能會得到相同的結果 計數 功能如下圖所示。 但是現在,您想要 總和 功能取代 計數 的功能,如何在Excel中一次更改多個數據透視表字段的計算?

文檔更改字段設置 1

手動一一更改樞紐分析表中的欄位設定

使用VBA代碼更改數據透視表中的多個字段設置


箭頭藍色右氣泡 手動一一更改樞紐分析表中的欄位設定

在Excel中,您可以通過在功能表中一一更改功能來更改字段設置的計算。 值字段設置 對話框,請按以下步驟操作:

1。 在“值”區域中選擇要為其更改數據透視表中的匯總功能的字段,然後單擊鼠標右鍵以選擇 值字段設置,請參見屏幕截圖:

文檔更改字段設置 1

2。 然後在 值字段設置 對話框中,選擇要使用的一種計算類型 總結價值 標籤,請參見屏幕截圖:

文檔更改字段設置 1

3。 然後點擊 OK 關閉此對話框,您可以看到您的Count函數已更改為Sum函數,請參見屏幕截圖:

文檔更改字段設置 1

4。 重複上述步驟,一個接一個地更改其他字段的計算設置。

注意: 您還可以通過右鍵單擊一個字段單元格並選擇來更改字段設置 匯總值 並從上下文菜單中選擇您需要的一種計算。

文檔更改字段設置 1


箭頭藍色右氣泡 使用VBA代碼更改數據透視表中的多個字段設置

如果需要更改許多字段計算,上述方法將既費時又繁瑣,在這裡,我可以向您介紹一個代碼,以同時更改多個字段設置。

1。 單擊數據透視表中的任何單元格。

2。 按住 ALT + F11 鍵,然後打開 Microsoft Visual Basic for Applications窗口.

3。 點擊 插入 > 模塊,然後將以下代碼粘貼到 模塊窗口.

VBA代碼:更改數據透視表中的多個字段設置

Public Sub SetDataFieldsToSum()
'Update 20141127
Dim xPF As PivotField
Dim WorkRng As Range
Set WorkRng = Application.Selection
With WorkRng.PivotTable
   .ManualUpdate = True
   For Each xPF In .DataFields
      With xPF
         .Function = xlSum
         .NumberFormat = "#,##0"
      End With
   Next
   .ManualUpdate = False
End With
End Sub

4。 然後按 F5 鍵執行此代碼,並且所選數據透視表中的所有字段設置都已立即轉換為您的需要計算,請參見屏幕截圖:

文檔更改字段設置 1
-1
文檔更改字段設置 6

備註:在上面的代碼中,您可以根據需要將Sum函數更改為任何其他計算,例如Average,Max,Min。 您只需要在以下腳本中更改總和: 公共子SetDataFieldsToSum() 函數= xlSum 其他功能。

最佳辦公生產力工具

🤖 Kutools 人工智慧助手:基於以下內容徹底改變數據分析: 智慧執行   |  生成代碼  |  建立自訂公式  |  分析數據並產生圖表  |  呼叫 Kutools 函數...
熱門特色: 尋找、突出顯示或識別重複項   |  刪除空白行   |  合併列或儲存格而不遺失數據   |   沒有公式的回合 ...
超級查詢: 多條件VLookup    多值VLookup  |   跨多個工作表的 VLookup   |   模糊查詢 ....
高級下拉列表: 快速建立下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
欄目經理: 新增特定數量的列  |  移動列  |  切換隱藏列的可見性狀態  |  比較範圍和列 ...
特色功能: 網格焦點   |  設計圖   |   大方程式酒吧    工作簿和工作表管理器   |  資源庫 (自動文字)   |  日期選擇器   |  合併工作表   |  加密/解密單元格    按清單發送電子郵件   |  超級濾鏡   |   特殊過濾器 (過濾粗體/斜體/刪除線...)...
前 15 個工具集12 文本 工具 (添加文本, 刪除字符,...)   |   50+ 圖表 類型 (甘特圖,...)   |   40+ 實用 公式 (根據生日計算年齡,...)   |   19 插入 工具 (插入二維碼, 從路徑插入圖片,...)   |   12 轉化 工具 (數字到單詞, 貨幣兌換,...)   |   7 合併與拆分 工具 (高級合併行, 分裂細胞,...)   |   ... 和更多

使用 Kutools for Excel 增強您的 Excel 技能,體驗前所未有的效率。 Kutools for Excel 提供了 300 多種進階功能來提高生產力並節省時間。  點擊此處獲取您最需要的功能...

產品描述


Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!
Comments (29)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Brilliant, thanks, this worked perfectly and save much time
This comment was minimized by the moderator on the site
Muchas gracias por el aporte
This comment was minimized by the moderator on the site
Hi!

I'm wondering how to use the code for Count Numbers instead of count, I've tried changing the code to Count Numbers and CountNumbers but netiher works..

Help is appreciated :)
This comment was minimized by the moderator on the site
Hello, Sara,
Sorry for that your Count Numbers field can't be changed in the code, so, you need to change the field one by one with the first method in this article.
Thank you!
This comment was minimized by the moderator on the site
Thank you so much, this VBA code works amazing. 
This comment was minimized by the moderator on the site
I got the error" Unable to get the PivotTable property of the range class". Do you know hoe can I fix this?
This comment was minimized by the moderator on the site
I got this error, "Unable to get the Pivot Table property of the Range class". Do you know how I can fix this?
This comment was minimized by the moderator on the site
Thank you so much!!! That is sooo helpful and brilliant! Saved me soooo much time
This comment was minimized by the moderator on the site
Thank you so much for the VBA code, I LOVE it!
This comment was minimized by the moderator on the site
Thank you. The code worked beautifully and saved me a lot of wasted time and energy!
This comment was minimized by the moderator on the site
I've used the VBA solution with great success in the past, but it doesn't work with the data model. Do you know if there is a solution for that?
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations