2016-01-07 52 views
0

我已經經歷了多個Q &作爲錯誤處理VBA閱讀,但由於某種原因,我的代碼不能正常工作。我試圖過濾一個特定的單詞(變量名稱「item」),如果過濾的範圍沒有該單詞,它必須跳過該代碼並跳轉到錯誤處理程序(並轉到下一個單詞)。這必須繼續,直到完成。我的代碼如下:錯誤處理程序在VBA 2016

Sub Group_Button1_Click() 

    Worksheets("Grouping_Name").Select 
    Range("B2").Select 
    Range(Selection, Selection.End(xlDown)).Select 
    lastrow_grouping = Selection.Count + 1 


Dim filterRange As Range 
Dim copyRange As Range 
Dim lastrow_To_be_grouped As Long 

For grouping_counter = 2 To lastrow_grouping 

' on to_be_grouped sheet turn off any autofilters that are already set Sheets("To_be_grouped").AutoFilterMode = False 
' find the last row with data in column A of To_be_grouped sheet 
lastrow_To_be_grouped = Sheets("To_be_grouped").Range("A" & Sheets("To_be_grouped").Rows.Count).End(xlUp).Row 
' the range that we are auto-filtering (all columns) 
Set filterRange = Sheets("To_be_grouped").Range("A1:B" & lastrow_To_be_grouped) 
' Copy filtered data (exclude header) in To_be_grouped sheet 
Set copyRange = Sheets("To_be_grouped").Range("A2:B" & lastrow_To_be_grouped) 

' Assign variable "item" which is the word to filter 
Item = Sheets("Grouping_Name").Range("A" & grouping_counter).Value 
' Filter column with variable item 
filterRange.AutoFilter field:=2, Criteria1:="=*" & Item & "*", Operator:=xlAnd 

' copy the visible cells to Final_Grouping sheet 
' determine first row with no data in Final Grouping sheet and copy to sheet 
On Error GoTo errorhandler 
' This is where I need help, if there is no line with the filtered word the code must jump to errorhandler 
If IsNumeric(copyRange.SpecialCells(xlCellTypeVisible).Count) = True Then 
copyRange.SpecialCells(xlCellTypeVisible).Copy Sheets("Final_Grouping").Range("A" & item_counter_total) 
' delete the tables 
Sheets("To_be_grouped").Range("$A$1:$B$" & lastrow_To_be_grouped).Offset(1, 0).SpecialCells _ 
(xlCellTypeVisible).EntireRow.Delete 

' Count the number of rows added 
item_counter = copyRange.SpecialCells(xlCellTypeVisible).Count/2 
' ------------------------------------------ 

For group_table_counter = item_counter_total To item_counter_total + item_counter - 1 Step 1 
    Sheets("Final_Grouping").Range("C" & group_table_counter).Value = Item 
Next group_table_counter 
' ------------------------------------------ 
item_counter_total = item_counter_total + item_counter 
End If 
errorhandler: 
Next grouping_counter 
End Sub 

任何援助將不勝感激。謝謝

+3

爲什麼你需要一個錯誤處理程序?難道你不能改變你的IF邏輯是如果像copyRange.SpecialCells(xlCellTypeVisible).Count> 0然後......當然,這還沒有經過測試,但應該做同樣的事情,對不對? – sous2817

+1

也澄清了一點:IsNumeric(copyRange。SpecialCells(xlCellTypeVisible).Count)= True將始終爲TRUE,因爲您要返回的計數爲0,這恰好是一個數字... – sous2817

回答

0

我無法確切知道您的代碼正在執行什麼操作,或者您嘗試捕獲的錯誤是什麼,但是您遇到的問題之一是您無法將「Next grouping_counter」放入錯誤處理器。

我認爲你的誤解是基於這樣一個事實,即如果你在「On Error GoTo ErrorHandler」語句中放置ErrorHandler中的代碼,將會在出現錯誤的情況下運行。

但是,當代碼被編譯時,VBA試圖確定代碼是否有意義,並且給你一個錯誤,說明你的下一條語句沒有For循環,因爲如果在你期望的地方發生錯誤它,你會在一個for循環,程序不知道這一點。

正如在評論中提到sous2817,你可能不需要的錯誤處理,但我將介紹兩種方法,以便您可以更好地瞭解如何做錯誤處理。

在這種情況下,你可以在一對夫婦的方式使用ErrorHandling中,假設在你的代碼中出現的錯誤:

1.

For grouping_counter = 2 To lastrow_grouping 
    'some code here 

    On Error GoTo ErrorHandler 
    'code that has error you are trying to handle 
    'more code to run 
NextGroupingCounter: 'label to jump to from error handler 

Next grouping_counter 


Exit Sub 

ErrorHandler: 

    GoTo NextGroupingCounter 

End Sub 

當然,這是如果你不除了跳過其餘的代碼之外,實際上想要對錯誤做任何事情。如果這是您想要的,那麼只需將On Error Code更改爲GoTo NextGroupingCOunter標籤!

2.

For grouping_counter = 2 To lastrow_grouping 
    'some code here 

    On Error GoTo NextGroupingCounter 
    'code that has error you are trying to handle 
    'more code to run 
NextGroupingCounter: 'label to jump to from error handler 

Next grouping_counter 

當然,你可能甚至不需要錯誤處理

3.

For grouping_counter = 2 To lastrow_grouping 
    'some code here 
    If [*insert text to check condition needed for no error*] Then 
      'code to run if no error 
    End If 
    'If the condition needed for no error is false, this will just go to the next one. 
Next grouping_counter 

選項3優選的是如果可能的。如果您的代碼中實際發生錯誤,則選項2將正常工作。如果你想捕獲錯誤,然後跳回到你的循環,選項3的作品。

此外,請注意我將「Exit Sub」放在ErrorHandler上方。 ErrorHandler標籤只是一個標籤,所以如果程序運行到結束,它將運行ErrorHandler部分中的代碼。 Exit Sub結束該過程以避免這種情況。

另請注意,對於其他情況,您可以使用「On Error Resume Next」來繼續發生錯誤,或者甚至在錯誤處理程序中將「Resume Next」放回到錯誤之一。這有點超出了問題的範圍,但可能會添加一些有用的信息。