我一直在寻找答案,存在许多相似但不相同的主题,而我找不到我的问题的答案。
我有一个宏,可以让我自动更改PVT的过滤器并刷新PVT。相应地将过滤器更改为一个单元格的值(在PVT过滤器中复制/粘贴该单元格的值)
最近,每次我将更多数据添加到与数据透视表相关的表后,否则我将无法再运行宏
“ Microsoft Excel已停止工作”。
VBA代码仍然相同,没有改变,所以我不太了解。
请在代码后找到:
Sub update_Filter()
Application.ScreenUpdating = False
ActiveSheet.Calculate
'Set the Variables to be used
Dim pt As PivotTable
Dim Field As PivotField
Dim NewFilter As String
'Here you amend to suit your data
Set pt = Worksheets("PVT Competition Data").PivotTables("PivotTable3")
Set Field = pt.PivotFields("PN ******")
NewFilter = Worksheets("PVT Competition Data").Range("B4").Value
On Error Resume Next
'This updates and refreshes the PIVOT table
With pt
Field.ClearAllFilters
Field.CurrentPage = NewFilter
pt.RefreshTable
Range("E12:I12").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("E12").Select
End With
End Sub
如果您发现此内容有问题或有任何解释,您将节省我的时间(并且肯定会发生以下事情...)。
也请问您是否需要更多详细信息。
谢谢!