excel - 如何修复第二个errHandler

标签 excel vba error-handling

我需要过滤两个不同的列,并且每次删除可见行。但是,可能没有要删除的任何行,因此我包含了errHandlers。在当前工作簿中,两次都没有行要删除。这个第一个实例工作正常,但是第二个实例抛出运行时错误1004。

以下是我的代码的一部分,其中包含麻烦的errHandlers:

Range("T1").Select
With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .Color = 10498160
    .TintAndShade = 0
    .PatternTintAndShade = 0
End With
   With Selection.Font
    .Color = -16711681
    .TintAndShade = 0
End With
With Selection
    .HorizontalAlignment = xlGeneral
    .VerticalAlignment = xlBottom
    .WrapText = True
    .Orientation = 0
    .AddIndent = False
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
End With
ActiveCell.FormulaR1C1 = "OK or DELETE"
Rows("1:1").Select
Selection.AutoFilter
Range("T2").Select
ActiveCell.FormulaR1C1 = _
    "=IF(AND(RC[-19]<>R[-1]C[-19],RC[-10]<>R[-1]C[-10]),""OK"",IF(AND(RC[-19]=R[-1]C[-19],RC[-10]<>R[-1]C[-10]),""OK"",IF(AND(RC[-19]=R[-1]C[-19],RC[-10]=R[-1]C[-10],RC[-7]=""T""),""OK"",""DELETE"")))"
Selection.AutoFill Destination:=Range("T2:T" & Cells(Rows.Count, 1).End(xlUp).Row)
Range("T2:T" & Cells(Rows.Count, 1).End(xlUp).Row).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Application.CutCopyMode = False
ActiveSheet.Range("$A$1:$T" & Cells(Rows.Count, 1).End(xlUp).Row).AutoFilter Field:=20, Criteria1:="DELETE"

On Error GoTo errHandler:

Selection.SpecialCells(xlCellTypeVisible).Select
Selection.EntireRow.Delete

errHandler:
    ActiveSheet.Range("$A$1:$T" & Cells(Rows.Count, 1).End(xlUp).Row).AutoFilter Field:=20
    Columns("E:G").Select
    Selection.ColumnWidth = 11
    Range("G2").Select
    ActiveCell.FormulaR1C1 = _
        "=IF(RC[6]=""T"",""DELETE"",IF(AND(RC[-6]=R[1]C[-6],RC[3]=R[1]C[3],R[1]C[6]=""T""),R[1]C[-1],IF(RC[-6]=R[1]C[-6],R[1]C[-1]-1,VALUE(""06/30/2017""))))"
    Selection.AutoFill Destination:=Range("G2:G" & Cells(Rows.Count, 1).End(xlUp).Row)
    Range("G2:G" & Cells(Rows.Count, 1).End(xlUp).Row).Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
      ActiveSheet.Range("$A$1:$T" & Cells(Rows.Count, 1).End(xlUp).Row).AutoFilter Field:=7, Criteria1:="DELETE"

    On Error GoTo errHandler2:

    Selection.SpecialCells(xlCellTypeVisible).Select
    Selection.EntireRow.Delete

errHandler2:
  ActiveSheet.Range("$A$1:$T" & Cells(Rows.Count, 1).End(xlUp).Row).AutoFilter Field:=7
  Range("D2").Select
    ActiveWorkbook.Save
End Sub

任何帮助将不胜感激。

最佳答案

您已经进入Spaghetti Lane,想要在深入了解之前先离开那里。

您的过程做了很多事情。做太多的事情是在过程范围内需要多个错误处理子例程的唯一原因。打破。东西下。

如果在VBA处于错误状态时执行On Error语句,则该语句不起作用-因此,任何错误处理子例程都不应包含任何On Error语句。

首先将“删除所有可见行”代码拉入其自己的过程范围:

Private Sub DeleteVisibleRows(ByVal source As Range)
    On Error Resume Next
    source.SpecialCells(xlCellTypeVisible).EntireRow.Delete
    On Error GoTo 0
End Sub

请注意,Selection与该代码无关,以及无需将Range编码为Select

其次,错误处理子例程只能在VBA处于错误状态时运行;不这样做会导致“快乐的执行路径”和“错误的执行路径”交织在一起,并且永远不会顺利结束。

On Error GoTo errHandler:开始,您的代码应如下所示:
DeleteVisibleRows Selection 'TODO: work out what Range object this is, use it instead

Dim entireTable As Range
Set entireTable = ActiveSheet.Range("$A$1:$T" & ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row)

entireTable.AutoFilter Field:=20
ActiveSheet.Columns("E:G").ColumnWidth = 11

Dim columnG As Range 'TODO: use meaningful name
Set columnG = ActiveSheet.Range("G2:G" & ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row)

'NOTE: no need to AutoFill if we write the whole column at once..
columnG.FormulaR1C1 = "=IF(RC[6]=""T"",""DELETE"",IF(...))"
'NOTE: no clipboard gets involved
columnG.Value = columnG.Value ' overwrites formulas with their values

entireTable.AutoFilter Field:=7, Criteria1:="DELETE"
DeleteVisibleRows entireTable

ActiveSheet.Range("D2").Select ' <~ this is the only legit use of Range.Select!!
ActiveWorkbook.Save

关于excel - 如何修复第二个errHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57859747/

相关文章:

excel - 将 Range.Address 输入到没有 ' ' 的公式中

mysql - 如何使用 Apache Drill 同时处理 Excel 文档和 RDBMS 中的源?

arrays - 分配给数组时的编译错误 - VBA (Excel)

excel - 从 Excel VBA 调用 Bloomberg BQL 查询

php - 将 PHP 错误处理程序限制在特定的命名空间

string - excel中如何合并折线

Excel VBA用函数替换但留下数字

VBA : Remove know password from multiple documents (improve code)

c++ - 对许多函数调用进行错误检查

error-handling - 仍未决定如何处理Swift 2中的错误