【问题标题】:Error Handler in VBA 2016VBA 2016 中的错误处理程序
【发布时间】:2016-01-07 19:34:21
【问题描述】:

我已经阅读了有关 VBA 中错误处理的多个问答,但由于某种原因,我的代码无法正常工作。我正在尝试过滤特定的单词(变量名“item”),如果过滤的范围没有单词,它必须跳过代码并跳转到错误处理程序(并转到下一个单词)。这必须持续到 for 完成。我的代码如下:

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

任何帮助将不胜感激。谢谢

【问题讨论】:

  • 为什么需要错误处理程序?您不能将 IF 逻辑更改为 If copyRange.SpecialCells(xlCellTypeVisible).Count > 0 then ...Granted,这还没有经过测试,但应该做同样的事情,对吧?
  • 另外,澄清一下: IsNumeric(copyRange.SpecialCells(xlCellTypeVisible).Count) = True 将始终为 TRUE,因为您返回的计数为 0,恰好是一个数字...

标签: excel vba error-handling


【解决方案1】:

我无法确定您的代码到底在做什么,或者您试图捕获的错误是什么,但您遇到的问题之一是您无法将“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 the 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”放在错误处理程序中以返回错误后的行。这有点超出了问题的范围,但可能会添加一些有用的信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-23
    • 2017-10-15
    • 1970-01-01
    • 2016-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多