2015-10-20 64 views
0

雖然主題是在那裏,在pivotfields錯誤1004方面,我只是沒有看到這種情況,我需要解決,並沒有任何線索如何。錯誤1004「無法從pivottable獲得pivotfield」

這是一個錄製的宏代碼:

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 

我記錄了它,而當運行宏... ERR 1004

這是記錄碼,所以我希望它運行像一個魅力。但不是。 err出現在第一行代碼中。

任何線索?提前致謝。

+1

也許'ActiveSheet'不是一個與數據透視表,如果你運行的宏? –

回答

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 
+1

你好。謝謝您的幫助。雖然我理解你的方法,但我可以手動驗證工作表,pivottable和pivotfield都存在。運行代碼時,我正在查看錶單。我相信,困難在於,如果Excel出於某種原因不認可這些對象之一。毫無疑問,你的代碼確實解決了調試部分。我在網上搜索了一些漫畫,但這些問題看起來像巫術或其他東西。不過謝謝。我會這樣做來嘗試找到問題。 – JDF