Err 1004"无法从枢轴表获得枢轴场"

时间:2015-10-20 08:01:50

标签: excel vba excel-vba pivot-table

尽管主题已经存在,但就枢轴场中的错误1004而言,我还没有看到这种情况,我需要解决这个问题,并且不知道如何解决这个问题。

这是录制的宏代码:

With ActiveSheet.PivotTables("SybusPivotTable").PivotFields("Lote")
    .PivotItems("0").visible = False
    .PivotItems("ERRO").visible = False
End With
With ActiveSheet.PivotTables("SybusPivotTable").PivotFields("Referência")
    .PivotItems("").visible = False
    .PivotItems("0").visible = False
End With
With ActiveSheet.PivotTables("SybusPivotTable").PivotFields("tipo_mov")
    .PivotItems("2").visible = False
End With

我录制了它,当运行宏时...... 错误1004.

它是一个录制的代码,所以我希望它像魅力一样运行。但不是。错误显示在第一行代码中。

任何线索?提前谢谢。

1 个答案:

答案 0 :(得分:1)

这应该可以帮助你找到错误的有罪方(我在每一行之后发表评论来解释错误的含义)。

试一试:

Sub test_JDF()
Dim Ws As Worksheet, _
    Pt As PivotTable, _
    Pf As PivotField

Set Ws = ActiveSheet
'Set Ws = ThisWorkbook.Sheets("Sheet_Name_To_Be_Replaced")
Set Pt = Ws.PivotTables(1) 'if error pops here, there is no pivottable on the active sheet
Set Pt = Ws.PivotTables("SybusPivotTable") 'if error pops here, there is no pivottable on the active sheet that is named "SybusPivotTable"
Set Pf = Pt.PivotFields(1) 'if error pops here, the pivottable is empty of pivotfields (highly unlikely)

Set Pf = Pt.PivotFields("Lote") 'if error pops here, there is no pivotfield name "Lote" in the pivottable
With Pf
    .PivotItems("0").Visible = False
    .PivotItems("ERRO").Visible = False
End With

Set Pf = Pt.PivotFields("Referência")
With Pf
    .PivotItems("").Visible = False
    .PivotItems("0").Visible = False
End With


With Pt.PivotFields("tipo_mov")
    .PivotItems("2").Visible = False
End With

End Sub