根据Excel中的两个单元格值更新两个数据透视表中的两个过滤器

时间:2019-07-18 15:21:31

标签: excel vba filter pivot excel-pivot

我正在尝试基于两个单元格在两个数据透视表中过滤两个过滤器。这个想法是用户在两个单元格中输入信息,这两个枢轴将自动更新(两个过滤器)。

我正在使用此代码,该代码仅用于更新每个数据透视表中的一个过滤器:

<StackTrace>
at OrderTemplateTool.Web.Common.TemplateExportManager.ExportInternal(Template template, Format format) in C:\WebProjects\COTT\COTT\OrderTemplateTool.Web\Common\TemplateExportManager.cs:line 94 at OrderTemplateTool.Web.Common.TemplateExportManager.ExportAndSave(Template template, Format format) in C:\WebProjects\COTT\COTT\OrderTemplateTool.Web\Common\TemplateExportManager.cs:line 116 at OrderTemplateTool.Web.Controllers.Api.FhirController.Read(String type, String id) in C:\WebProjects\COTT\COTT\OrderTemplateTool.Web\Controllers\Api\FhirController.cs:line 126 at lambda_method(Closure , Object , Object[] ) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()
</StackTrace>

但是,我还有第二个过滤器要使用的单元格d3。

我尝试了以下操作,但不起作用,仅更新了第一个过滤器:

Private Sub Worksheet_Change(ByVal Target As Range)

Dim xPTable As PivotTable
Dim xPFile As PivotField
Dim xStr As String
On Error Resume Next

If Intersect(Target, Range("d2")) Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set xPTable = Worksheets("Scenarios").PivotTables("Standard")
Set xPFile = xPTable.PivotFields("Material")
xStr = Target.Text
xPFile.ClearAllFilters
xPFile.CurrentPage = xStr
Application.ScreenUpdating = True

If Intersect(Target, Range("d2")) Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set xPTable = Worksheets("Scenarios").PivotTables("Tasks")
Set xPFile = xPTable.PivotFields("Material")
xStr = Target.Text
xPFile.ClearAllFilters
xPFile.CurrentPage = xStr
Application.ScreenUpdating = True

End Sub

结束子

我可能会缺少任何东西吗?我对VBA很陌生

预先感谢您的帮助。


你好,我知道了,请看下面:

私有子工作表_更改(按目标的ByVal目标) '由Extendoffice更新20180702     xPTable昏暗的数据透视表     昏暗的xPFile作为PivotField     Dim xStr作为字符串     关于错误继续下一个

Private Sub Worksheet_Change(ByVal Target As Range)

Dim xPTable As PivotTable
Dim xPFile As PivotField
Dim xStr As String
On Error Resume Next

If Intersect(Target, Range("d2")) Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set xPTable = Worksheets("Scenarios").PivotTables("Standard")
Set xPFile = xPTable.PivotFields("Material")
xStr = Target.Text
xPFile.ClearAllFilters
xPFile.CurrentPage = xStr
Application.ScreenUpdating = True

If Intersect(Target, Range("d2")) Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set xPTable = Worksheets("Scenarios").PivotTables("Tasks")
Set xPFile = xPTable.PivotFields("Material")
xStr = Target.Text
xPFile.ClearAllFilters
xPFile.CurrentPage = xStr
Application.ScreenUpdating = True

If Intersect(Target, Range("d3")) Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set xPTable = Worksheets("Scenarios").PivotTables("Standard")
Set xPFile = xPTable.PivotFields("Resource")
xStr = Target.Text
xPFile.ClearAllFilters
xPFile.CurrentPage = xStr
Application.ScreenUpdating = True

If Intersect(Target, Range("d3")) Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set xPTable = Worksheets("Scenarios").PivotTables("Tasks")
Set xPFile = xPTable.PivotFields("Resource")
xStr = Target.Text
xPFile.ClearAllFilters
xPFile.CurrentPage = xStr
Application.ScreenUpdating = True

结束子

0 个答案:

没有答案