excel - VBA使用选定的列过滤器创建一个新的数据透视表
问题描述
我想做的是:
如果单元格“J5”的值发生变化,那么我的数据透视表会创建一个新的:
动作过滤器“Actionstr”
SERVICE 列,只有一列“Servicestr”
Private Sub Worksheet_Change(ByVal Target As Range)
Dim xPTable As PivotTable
Dim ActionStr As String
Dim ServiceStr As String
On Error Resume Next
If Intersect(Target, Range("J5")) Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set xPTable = Worksheets("centrum1").PivotTables("test")
ActionStr = Range("G5").Text
ServiceStr = Range("G2").Text
With xPTable
.ManualUpdate = False
.ClearAllFilters
.PivotFields("ACTION").PivotFilters.Add Type:=xlCaptionEquals, Value1:=ActionStr
.PivotFields("SERVICE").PivotFilters.Add Type:=xlCaptionEquals, Value1:=ServiceStr
.ManualUpdate = True
End With
Application.ScreenUpdating = True End sub
我这样做只是为了“过滤器”
Private Sub Worksheet_Change(ByVal Target As Range)
Dim xPTable As PivotTable
Dim xPFile As PivotField
Dim Actionstr As String
On Error Resume Next
If Intersect(Target, Range("J5")) Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set xPTable = Worksheets("centrum1").PivotTables("test")
Set xPFile = xPTable.PivotFields("ACTION")
Actionstr = Range("G5").Text
xPFile.ClearAllFilters
xPFile.CurrentPage = Actionstr
Application.ScreenUpdating = True End Sub
但它只会更改“过滤器”,它不会创建基于 Servicestr 的列。
感谢您的任何提示。
解决方案
推荐阅读
- android - 从 Firebase 数据中检查和比较应用中的特定数据
- python - Python中的快速排序抛出RecursionError
- reactjs - 可以获取不同组件的父组件
- dart - Flutter:RenderBox 没有布局
- java - 尝试在 Java 运行时从 jar 文件加载类时出现 ClassNotFoundException
- git - 合并两个分支后,一些文件更改为旧版本,一些文件被删除
- mysql - 在 Docker 中配置 mysql
- java - 我想使用 eclipse gui,但我得到了错误的 java 版本
- c# - 带返回的空合并运算符 (??)
- http - 如何将 net/http.ResponseWriter 转换为 net/http.Response