我正在研究選擇數據透視表的一部分併發送該提取的過程。VBA - GetPivotData - 無法捕獲運行時錯誤1004
一切正常,但我一直有一個運行時錯誤1004我不能趕上(,以避免它,並不斷循環)等我的循環不順暢工作...
這裏是有問題的一部分:
On Error GoTo 0
On Error GoTo NextSale
If IsError(pt.GetPivotData("Amount", Pf.Name, Pi.Name, "Sales_Opp", PiO.Name)) Then GoTo NextSale
Set Rg = pt.GetPivotData("Amount", Pf.Name, Pi.Name, "Sales_Opp", PiO.Name)
On Error GoTo 0
Set RgT = Union(RgT, Rg)
NextSale:
因爲pt.GetPivotData("Amount", Pf.Name, Pi.Name, "Sales_Opp", PiO.Name)
將拋出一個運行時錯誤1004,當組合中不存在數據,我只是想避免被封鎖在洛杉磯歡聲笑語。
我搜索了,但沒有設法解決這個問題... IsError()
和On Error GoTo
不起作用。我甚至在選項選中(工具 - >選項 - >常規 - >錯誤捕獲),我已經很上Break on Unhandled Errors
...
下面是完整的代碼:
Sub testPt()
Dim Pt As PivotTable, _
Pf As PivotField, _
Pi As PivotItem, _
PiO As PivotItem, _
Ws As Worksheet, _
TpStr As String, _
RgT As Range, _
Rg As Range
Set Ws = ThisWorkbook.Sheets("PT_All")
For Each Pt In Ws.PivotTables
For Each Pf In Pt.PivotFields
If Pf.Name <> "Sales" Then
Else
For Each Pi In Pf.PivotItems
Set RgT = Pi.LabelRange
For Each PiO In Pt.PivotFields("Sales_Opp").PivotItems
On Error GoTo 0
On Error GoTo NextSale
If IsError(Pt.GetPivotData("Amount", Pf.Name, Pi.Name, "Sales_Opp", PiO.Name)) Then GoTo NextSale
Set Rg = Pt.GetPivotData("Amount", Pf.Name, Pi.Name, "Sales_Opp", PiO.Name)
On Error GoTo 0
Set RgT = Union(RgT, Rg)
NextSale:
Next PiO
RgT.Select
MsgBox RgT.Address
Next Pi
End If
Next Pf
Next Pt
End Sub
Thx爲您的輸入,我不知道有關將退出錯誤處理的'Resume'部分,以便完成一些調整,我學會了一個關於錯誤處理非常有用,非常感謝! ;) – R3uK