【问题标题】:Err 1004 "Cannot get pivotfield from pivottable"Err 1004 “无法从数据透视表中获取数据透视字段”
【发布时间】:2015-10-20 17:24:11
【问题描述】:

尽管主题已经存在,但就数据透视字段中的错误 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。

这是一个记录的代码,所以我希望它能够像魅力一样运行。但不是。错误出现在第一行代码中。

有什么线索吗?提前致谢。

【问题讨论】:

  • 如果您运行宏,ActiveSheet 可能不是带有数据透视表的那个?

标签: vba excel pivot-table


【解决方案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

【讨论】:

  • 您好。谢谢您的帮助。尽管我了解您的方法,但我可以手动验证工作表、数据透视表和数据透视字段是否都存在。运行代码时,我正在查看工作表。我认为,困难在于 Excel 是否出于任何原因无法识别这些对象之一。毫无疑问,您的代码确实解决了调试部分。我在网上搜索了一些答案,但问题似乎是巫术之类的。不过还是谢谢。我会这样做,试图找到问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-07
  • 1970-01-01
  • 1970-01-01
  • 2019-09-29
相关资源
最近更新 更多