跳到主要内容

如何更改数据透视表中的多个字段设置?

在工作表中创建数据透视表时,将字段拖到 价值观 列出 数据透视表字段列表,您可能会得到相同的结果 计数 功能如下图所示。 但是现在,您想要 总和 功能取代 计数 的功能,如何在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 Tab 为 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