如何基于Excel中的特定单元格值筛选数据透视表?
通常,我们通过在下拉列表中选择项目来过滤数据透视表中的数据,如下面的屏幕截图所示。 实际上,您可以根据特定单元格中的值过滤数据透视表。 本文中的 VBA 方法将帮助您解决问题。
使用VBA代码根据特定单元格值过滤数据透视表
下面的VBA代码可以帮助您基于Excel中的特定单元格值过滤数据透视表。 请执行以下操作。
1.请输入一个值,您将根据该值预先将数据透视表过滤到一个单元格中(这里我选择单元格H6)。
2.打开包含要根据单元格值过滤的数据透视表的工作表。 然后右键单击工作表选项卡,然后从上下文菜单中选择“查看代码”。 看截图:
3.在开幕 Microsoft Visual Basic应用程序 窗口,将下面的VBA代码复制到“代码”窗口中。
VBA代码:根据单元格值过滤数据透视表
Private Sub Worksheet_Change(ByVal Target As Range)
'Update by Extendoffice 20180702
Dim xPTable As PivotTable
Dim xPFile As PivotField
Dim xStr As String
On Error Resume Next
If Intersect(Target, Range("H6:H7")) Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set xPTable = Worksheets("Sheet1").PivotTables("PivotTable2")
Set xPFile = xPTable.PivotFields("Category")
xStr = Target.Text
xPFile.ClearAllFilters
xPFile.CurrentPage = xStr
Application.ScreenUpdating = True
End Sub
:在代码中,
1)“Sheet1”是工作表的名称。
2)“数据透视表2”是数据透视表的名称。
3)数据透视表中的过滤字段称为“产品分类".
4)您要过滤数据透视表的值放在单元格中 H6.
您可以根据需要更改上述变量值。
4。 按 其他 + Q 关闭键 Microsoft Visual Basic应用程序 窗口。
然后,数据透视表将根据单元格H6中的值进行过滤,如下图所示:
您可以根据需要将单元格值更改为其他值。
备注:您在单元格H6中键入的值应与数据透视表的“类别”下拉列表中的值完全匹配。
相关文章:
最佳办公生产力工具
热门特色: 查找、突出显示或识别重复项 | 删除空白行 | 合并列或单元格而不丢失数据 | 不使用公式进行四舍五入 ... | |
超级查询: 多条件VLookup | 多值VLookup | 跨多个工作表的 VLookup | 模糊查询 .... | |
高级下拉列表: 快速创建下拉列表 | 依赖下拉列表 | 多选下拉列表 .... | |
列管理器: 添加特定数量的列 | 移动列 | 切换隐藏列的可见性状态 | 比较范围和列 ... | |
特色功能: 网格焦点 | 设计图 | 大方程式酒吧 | 工作簿和工作表管理器 | 资源库 (自动文本) | 日期选择器 | 合并工作表 | 加密/解密单元格 | 按列表发送电子邮件 | 超级筛选 | 特殊过滤器 (过滤粗体/斜体/删除线...)... | |
前 15 个工具集: 12 文本 工具 (添加文本, 删除字符,...) | 50+ 图表 类型 (甘特图,...) | 40+ 实用 公式 (根据生日计算年龄,...) | 19 插入 工具 (插入二维码, 从路径插入图片,...) | 12 转化 工具 (小写金额转大写, 货币兑换,...) | 7 合并与拆分 工具 (高级组合行, 分裂细胞,...) | ... 和更多 |
增强您的 Excel 技能 Kutools for Excel,体验前所未有的效率。 Kutools for Excel 提供 300 多种高级功能,可提高工作效率并节省时间。 单击此处获取您最需要的功能...
Office Tab 为 Office 带来选项卡式界面,让您的工作更轻松
- 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
- 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
- 每天将您的工作效率提高50%,并减少数百次鼠标单击!

Sort comments by

#42324
This comment was minimized by the moderator on the site
0
0

#42387
This comment was minimized by the moderator on the site
Report
0
0

#42309
This comment was minimized by the moderator on the site
0
0

#39652
This comment was minimized by the moderator on the site
0
0

#39718
This comment was minimized by the moderator on the site
Report
0
0

#38840
This comment was minimized by the moderator on the site
0
0

#38901
This comment was minimized by the moderator on the site
Report
0
0

#38505
This comment was minimized by the moderator on the site
0
0

#35785
This comment was minimized by the moderator on the site
0
0

#35024
This comment was minimized by the moderator on the site
0
0

#34609
This comment was minimized by the moderator on the site
Report
0
0

#34435
This comment was minimized by the moderator on the site
0
0

#34436
This comment was minimized by the moderator on the site
0
0

#33770
This comment was minimized by the moderator on the site
0
0
There are no comments posted here yet