gpt4 book ai didi

excel - 如何修复第二个errHandler

转载 作者:行者123 更新时间:2023-12-03 08:44:20 25 4
gpt4 key购买 nike

我需要过滤两个不同的列,并且每次删除可见行。但是,可能没有要删除的任何行,因此我包含了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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com