Excel VBA - 跳過行/前往指令的程式碼

Excel VBA - 跳過行/前往指令的程式碼

我已經在 Excel 中建立了一段程式碼,用於從資料透視表中獲取資料並將其插入到圖表中,直接連結到該表的資料透視圖不會為我帶來我想要的可操作性。我之所以費盡心思編寫如此「複雜」的程式碼,是因為對於每個工廠和測試資訊組合,我需要將其作為圖表中的單獨條目。

因此,此程式碼的要點是遍歷每個工廠和測試資訊組合(命令的嵌套),然後將資料插入圖表中。我的使用者不會更改 x 和 y 的列位置,因此偏移量正常運作。

我的問題是,如果工廠/測試資訊組合不存在,它無論如何都會將其輸入到圖表中。當我嘗試使用 goto 命令並使用錯誤句柄將其發送到 Next PI2 時,它不起作用(可能是因為嵌套的 if 命令)。我四處尋找一個命令,可以將我的程式碼發送到程式碼中的特定行(即在圖形命令之後),但沒有任何運氣...

有誰知道在發生錯誤時跳到特定行的方法?

我添加了一組命令以在下一個PI2 處恢復,其中我說錯誤時轉到errhandler,然後從errhandler 轉到下一次迭代,但是當我運行代碼並收到錯誤時,它不會通過這條路線,而是而不是停在「相交」線。

Sub CreatePivotChart()

 Dim PF1 As PivotField
 Dim PI1 As PivotItem
 Dim PI2 As PivotItem
 Dim PF2 As PivotField
 Dim chartcount As Integer
 Dim pt As PivotTable
 Set pt = Worksheets("Pivot Table").PivotTables("PivotTable")

'set up pivot field locations 1 - plant and unit , 2 - test conditions

 Set PF1 = Worksheets("PivotTable").PivotTables("PivotTable").PivotFields("Plant")

 Set PF2 = Worksheets("Pivot Table").PivotTables("PivotTable").PivotFields("Test Info")

 'clear the chart from previous run
  chartcount = 0
  Sheets("Pivot Table Graph").ChartObjects("Chart 1").Chart.ChartArea.ClearContents

  On Error GoTo ErrHandler

 'find each visible unit
  For Each PI1 In PF1.PivotItems

  If PI1.Visible = True Then
     Unit = PI1.Name

     For Each PI2 In PF2.PivotItems

     'for each unit and test condition find the information at their intersection
       If PI2.Visible = True Then
       TC = PI2.Name


    'find the information that corresponds to each unit/test condition combination
    Intersect(pt.PivotFields("Plant").PivotItems(Unit).DataRange.EntireRow, pt.PivotFields("Test Info").PivotItems(TC).DataRange).Select
    Selection.Offset(-1, 0).Select
    ForXRanges = "='Pivot Table'!" & Selection.Address
    Selection.Offset(0, 1).Select
    ForYRanges = "='Pivot Table'!" & Selection.Address
    ForRangesName = Unit & "_" & TC

    'for each combination create a new series on the chart
    chartcount = chartcount + 1
    Sheets("Pivot Table Graph").ChartObjects("Chart 1").Activate
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(chartcount).Name = ForRangesName
    ActiveChart.SeriesCollection(chartcount).XValues = ForXRanges
    ActiveChart.SeriesCollection(chartcount).Values = ForYRanges

End If

NextIteration:
Next PI2

End If
Next PI1

Exit Sub

ErrHandler:
Resume NextIteration:

End Sub

答案1

更好的方法是使用 if 語句測試您的數據,以確保您的資料有效。如果不是,請不要繼續執行可能產生錯誤的程式碼區塊。

在您的示例中,這可能有效...更改此:

'find the information that corresponds to each unit/test condition combination
Intersect(pt.PivotFields("Plant").PivotItems(Unit).DataRange.EntireRow, pt.PivotFields("Test Info").PivotItems(TC).DataRange).Select
Selection.Offset(-1, 0).Select
ForXRanges = "='Pivot Table'!" & Selection.Address
Selection.Offset(0, 1).Select
ForYRanges = "='Pivot Table'!" & Selection.Address
ForRangesName = Unit & "_" & TC

'for each combination create a new series on the chart
chartcount = chartcount + 1
Sheets("Pivot Table Graph").ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(chartcount).Name = ForRangesName
ActiveChart.SeriesCollection(chartcount).XValues = ForXRanges
ActiveChart.SeriesCollection(chartcount).Values = ForYRanges

對此:

'find the information that corresponds to each unit/test condition combination
Set isect = Application.Intersect(pt.PivotFields("Plant").PivotItems(Unit).DataRange.EntireRow, pt.PivotFields("Test Info").PivotItems(TC).DataRange)

If isect Is Nothing Then 
    'Msgbox "Ranges do not intersect"
Else
    isect.Select 

    Selection.Offset(-1, 0).Select
    ForXRanges = "='Pivot Table'!" & Selection.Address
    Selection.Offset(0, 1).Select
    ForYRanges = "='Pivot Table'!" & Selection.Address
    ForRangesName = Unit & "_" & TC

    'for each combination create a new series on the chart
    chartcount = chartcount + 1
    Sheets("Pivot Table Graph").ChartObjects("Chart 1").Activate
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(chartcount).Name = ForRangesName
    ActiveChart.SeriesCollection(chartcount).XValues = ForXRanges
    ActiveChart.SeriesCollection(chartcount).Values = ForYRanges
End If

我無法測試這個,因為我沒有你的工作簿,但如果它不起作用,它應該會示範該方法。

答案2

您可以在不goto使用 VBA 的情況下處理錯誤,如下所示:

Sub ErrorHandling()
Dim A, d

On Error Resume Next    
REM Line that throws an error
A = A / 0
REM Store details about your error before it gets cleared
d = Err.Description

On Error GoTo 0

REM You see and can handle your error message here
MsgBox d  
End Sub

On Error Resume Next禁用拋出錯誤

On Error GoTo 0啟用拋出錯誤並清除Err對象

答案3

我最終回答了自己的問題,透過繼續瀏覽舊帖子等,我發現http://www.cpearson.com/excel/errorhandling.htm非常有幫助。

事實證明,我試圖使用兩個 goto 命令,先轉到錯誤處理程序,然後轉到下一個迭代。我需要做的是改變第二個轉到,恢復。

感謝大家的幫助,上面的程式碼完美運行!

相關內容