提示:其他語言是Google翻譯的。 你可以訪問 English 版本。
登入
x
or
x
x
註冊
x

or

如何將多個字段添加到數據透視表中?

當我們創建數據透視表時,我們需要將這些字段逐個手動拖動到行標籤或值中。 如果我們有一長串字段,我們可以快速添加幾行標籤,但其餘字段應該添加到值區域。 是否有任何快速方法讓我們只需在數據透視表中點擊一下,即可將所有其他字段添加到Value區域中?

使用VBA代碼將多個字段添加到數據透視表的值區域

Office選項卡在Office中啟用選項卡式編輯和瀏覽,使您的工作更輕鬆......
Kutools for Excel解決了您的大多數問題,並使您的生產率提高了80%
  • 重用任何東西: 將最常用或最複雜的公式,圖表和其他任何內容添加到您的收藏夾中,並在將來快速重複使用它們。
  • 超過20文本功能: 從文本字符串中提取數字; 提取或刪除部分文字; 將數字和貨幣轉換為英文單詞。
  • 合併工具:將多個工作簿和工作表合二為一; 合併多個單元格/行/列,而不會丟失數據; 合併重複的行和總和。
  • 拆分工具:根據價值將數據分割成多個工作表; 一本工作簿可轉換為多個Excel,PDF或CSV文件; 一列到多列。
  • 粘貼跳過 隱藏/過濾行; 數和總和 按背景顏色; 將個性化電子郵件批量發送給多個收件人。
  • 超級過濾器: 創建高級過濾方案並應用於任何工作表; 分類 按週,日,頻率等; 過濾 通過大膽,公式,評論......
  • 超過300強大的功能; 與Office 2007-2019和365一起使用; 支持所有語言; 在您的企業或組織中輕鬆部署。

箭頭藍色右泡 使用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。 支持所有語言。 在您的企業或組織中輕鬆部署。 全功能30天免費試用。
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?