提示:其它语言是由 Google 机器翻译的。 你可以访问 English 版本。
登录
x
or
x
x
马上登记
x

or

如何将多个字段添加到数据透视表中?

当我们创建数据透视表时,我们需要将这些字段逐个手动拖动到行标签或值中。 如果我们有一个很长的字段列表,我们可以快速添加几行标签,但剩余的字段应该被添加到值区域。 是否有任何快速的方法,我们只需点击数据透视表中的所有其他字段添加到值区域?

使用VBA代码将多个字段添加到数据透视表的值区域

Office选项卡在Office中启用选项卡式编辑和浏览,使您的工作更轻松......
Kutools for Excel - 最佳办公生产力工具将解决您的大部分Excel问题
  • 重用任何东西: 将最常用或最复杂的公式,图表和其他任何内容添加到您的收藏夹中,并在将来快速重复使用它们。
  • 超过20文本功能: 从文本字符串中提取数字; 提取或删除部分文本; 将数字和货币转换为英语单词...
  • 合并工具:多个工作簿和表格合二为一; 合并多个单元格/行/列而不丢失数据; 合并重复行和总和...
  • 拆分工具:根据价值将数据拆分为多个表格; 一个工作簿到多个Excel,PDF或CSV文件; 一列到多列......
  • 粘贴跳过 隐藏/过滤行; 数和总和 按背景颜色; 创建邮件列表和 通过Cell的价值发送电子邮件...
  • 超级过滤器: 创建高级过滤方案并应用于任何工作表; 排序 按周,日,频率等; 筛选 通过大胆,公式,评论......
  • 超过300强大的功能; 适用于Office 2007-2019和365; 支持所有语言; 在公司轻松部署; 全功能60天免费试用。

箭头蓝色右泡 使用VBA代码将多个字段添加到数据透视表的值区域


不幸的是,我们没有复选框来快速检查数据透视表字段列表中的所有字段,只需点击一下,但使用下面的VBA代码,它可以帮助您将其余字段一次性添加到值区域。 请做如下操作:

1. 在创建数据透视表之后,首先,您应该根据需要添加行标签字段,并将值字段留在 选择要添加到报告的字段 列表,看截图:</ p>

DOC-附加多领域,1

2。 按住 ALT + F11 键打开 Microsoft Visual Basic for Applications窗口.

3。 点击 插页 > 模块,并将以下代码粘贴到 模块窗口.

VBA代码:将多个字段添加到活动工作表中数据透视表的值区域中

Sub AddAllFieldsValues()
'Update 20141112
    Dim pt As PivotTable
    Dim I As Long
    For Each pt In ActiveSheet.PivotTables
        For I = 1 To pt.PivotFields.Count
            With pt.PivotFields(I)
              If .Orientation = 0 Then .Orientation = xlDataField
            End With
        Next
    Next
End Sub

4。 然后按 F5 键运行此代码,所有其余字段已同时添加到值区域,请参阅屏幕截图:

DOC-附加多领域,1

注意:此VBA代码应用于活动工作表的所有数据透视表。


相关文章:

如何列出工作簿中的所有数据透视表?

如何检查数据透视表是否存在于工作簿中?


Kutools for Excel - 最佳办公生产力工具提高80%的生产力

  • 重用: 快速插入 复杂的公式,图表 以及你以前用过的任何东西; 加密单元格 密码; 创建邮件列表 并发送电子邮件...
  • 超级方程式酒吧 (轻松编辑多行文字和公式); 阅读布局 (轻松读取和编辑大量单元格); 粘贴到过滤范围...
  • 合并单元格/行/列 不丢失数据; 分裂细胞含量; 组合重复的行/列...防止重复的细胞; 比较范围...
  • 选择复制或唯一 行; 选择空行 (所有细胞都是空的); 超级查找和模糊查找 在许多工作簿中; 随机选择......
  • 精确复制 多个单元格而不更改公式参考; 自动创建参考 多张表; 插入项目符号,复选框等等......
  • 提取文本,添加文本,按位置删除, 删除空间; 创建和打印分页小计; 在单元格内容和注释之间转换...
  • 超级过滤器 (将过滤方案保存并应用到其他工作表); 高级排序 按月/周/日,频率等; 特殊过滤器 用粗体,斜体......
  • 结合工作簿和工作表; 根据键列合并表; 将数据拆分为多个表格; 批量转换xls,xlsx和PDF...
  • 超过300强大的功能。 支持Office / Excel 2007-2019和365。 支持所有语言。 在您的企业或组织中轻松部署。 全功能60天免费试用。
kte tab 201905

Office选项卡为Office提供选项卡式界面,使您的工作更轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,Publisher,Access,Visio和Project。
  • 在同一窗口的新选项卡中打开并创建多个文档,而不是在新窗口中。
  • 通过50%提高您的工作效率,每天为您减少数百次鼠标点击!
官方底部

Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Neville · 1 years ago
    Works like a charm. Thanks
  • To post as a guest, your comment is unpublished.
    Zeeshan · 1 years ago
    Hi.

    It gives me a compile error when I click on Run

    Please help
  • To post as a guest, your comment is unpublished.
    Shaun Sheys · 1 years ago
    Does "xlDataField" not work with PowerPivot? I've got the code below, which is used to add all measures to the pivot table. It works with "xlColumnField" and "xlRowField". Any ideas how to make this work?

    Sub AddAllFieldsValues()
    Dim pt As PivotTable
    Dim iCol As Long
    Dim iColEnd As Long

    Set pt = ActiveSheet.PivotTables(1)

    With pt

    iCol = 1
    iColEnd = .CubeFields.Count

    For iCol = 1 To iColEnd
    With .CubeFields(iCol)
    If .Orientation = xlHidden Then
    .Orientation = xlDataField
    End If
    End With
    Next iCol

    End With

    End Sub
  • To post as a guest, your comment is unpublished.
    Sajal · 2 years ago
    This is great! Thanks.
  • To post as a guest, your comment is unpublished.
    sourav · 2 years ago
    Its takiung value as count. how can i convert all count as Sum
  • To post as a guest, your comment is unpublished.
    Nishant · 2 years ago
    Is there anyway if we want to add some selected columns from the base file instead of all.?
  • To post as a guest, your comment is unpublished.
    Shaun · 2 years ago
    Is there a way to run this only for excluded fields, so that when I run it I do not end up with duplicate fields? ie. "Field" and "Field_2"
    • To post as a guest, your comment is unpublished.
      Jeff · 2 years ago
      [quote name="Shaun"]Is there a way to run this only for excluded fields, so that when I run it I do not end up with duplicate fields? ie. "Field" and "Field_2"[/quote]

      Yes- two examples

      Use this to add all remaining unchecked items to ROWS

      Sub AddAllFieldsRow()
      Dim pt As PivotTable
      Dim iCol As Long
      Dim iColEnd As Long

      Set pt = ActiveSheet.PivotTables(1)

      With pt

      iCol = 1
      iColEnd = .PivotFields.Count

      For iCol = 1 To iColEnd
      With .PivotFields(iCol)
      If .Orientation = 0 Then
      .Orientation = xlRowField
      End If
      End With
      Next iCol

      End With

      End Sub

      Use this to add a remaining unchecked items to VALUES

      Sub AddAllFieldsValues()
      Dim pt As PivotTable
      Dim iCol As Long
      Dim iColEnd As Long

      Set pt = ActiveSheet.PivotTables(1)

      With pt

      iCol = 1
      iColEnd = .PivotFields.Count

      For iCol = 1 To iColEnd
      With .PivotFields(iCol)
      If .Orientation = 0 Then
      .Orientation = xlDataField
      End If
      End With
      Next iCol

      End With

      End Sub
      • To post as a guest, your comment is unpublished.
        Valerie · 2 years ago
        Yeah, to echo markus, is there a way to edit the code to only add unchecked fields that have certainly field labels (like with an if contains function)? For example, I have a question that is actually 50 yes/no values by topic and I'd like to move all of those to the values field. They all begin with the same question label q9_[specific question topic].
      • To post as a guest, your comment is unpublished.
        Markus · 2 years ago
        Is there a way to only add some ekstra fields, not the remaining or all of them, but just a pair of those who is not checked already? :)
  • To post as a guest, your comment is unpublished.
    Joe · 3 years ago
    This is fantastic. Can the macro be modified to add all to Row Labels instead of Values?
  • To post as a guest, your comment is unpublished.
    Razvan · 3 years ago
    Wow. It works very very well. Many thanks!
    Nonetheless, I am kinda amazed that Microsoft were not having a built-in feature for a rather simple task...
  • To post as a guest, your comment is unpublished.
    Diogo · 3 years ago
    Really nice and useful macro. However I had already added half of my fields into the Values and when I ran this macro it added all the same fields again. Any idea why?