错误1004“无法从pivottable获取pivotfield”

尽pipe这个话题已经出现了,但是就pivotfields中的错误1004而言,我只是没有看到这种情况,我需要解决这个问题,并且没有任何线索。

这是一个logging的macros代码:

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 

我logging它,当一个macros运行…错误1004。

这是一个录制的代码,所以我期望它像一个魅力运行。 但不是。 err出现在第一行代码中。

任何线索? 提前致谢。

这应该可以帮助你find错误的有罪方(我在每行之后解释了错误的含义)。

试一试 :

 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