我有这段代码:
Sub RvsP()
Dim objTable As PivotTable, objField As PivotField
Range("A1").Select
Set objTable = Sheet2.PivotTableWizard
Set objField = objTable.PivotFields("Real Resolution Provided to Client")
objField.Orientation = xlRowField
Set objField = objTable.PivotFields("Priority")
objField.Orientation = xlColumnField
Set objField = objTable.PivotFields("Priority")
objField.Orientation = xlDataField
Dim pf As PivotField
Set pf = ActiveSheet.PivotTables(1).PivotFields("Real Resolution Provided to Client")
pf.DataRange.Cells(1).Group Start:=True, End:=True, Periods:=Array(False, False, _
False, False, True, False, False)
End Sub
枢轴运行平稳。
但问题是当我打开一个新的工作簿时,即使它的工作表中没有数据,当我运行这个宏时,生成的最后一个数据透视数据仍然存在。
如何使用VBA代码完全清除它们?
答案 0 :(得分:1)
您需要将“Missing Item Limit”设置为None(也可以在Pivot Table的属性中手动完成),并使用以下行:
objTable.PivotCache.MissingItemsLimit = xlmissingItemNone
objTable.PivotCache.Refresh
很抱歉法语截图,我无法在该计算机上切换语言,但这里是手动选项的位置:
Number of items to retain ...
设置为None
所以测试一下:
Sub RvsP()
Dim objTable As PivotTable, objField As PivotField
Range("A1").Select
Set objTable = Sheet2.PivotTableWizard
objTable.PivotCache.MissingItemsLimit = xlmissingItemNone
objTable.PivotCache.Refresh
Set objField = objTable.PivotFields("Real Resolution Provided to Client")
objField.Orientation = xlRowField
Set objField = objTable.PivotFields("Priority")
objField.Orientation = xlColumnField
objField.Orientation = xlDataField
Set objField = ActiveSheet.PivotTables(1).PivotFields("Real Resolution Provided to Client")
objField.DataRange.Cells(1).Group _
Start:=True, _
End:=True, _
Periods:=Array(False, False, False, False, True, False, False)
End Sub
答案 1 :(得分:0)
Set objTable = Sheet2.PivotTableWizard
你提到了Sheet2。所以它从sheet2中获取值。 检查sheet2中的数据
将其更改为
Set objTable = Sheet3.PivotTableWizard
请检查项目窗口