Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA错误处理设置转到特定行或标签?_Excel_Vba - Fatal编程技术网

Excel VBA错误处理设置转到特定行或标签?

Excel VBA错误处理设置转到特定行或标签?,excel,vba,Excel,Vba,我试图找出处理可能出现错误弹出的情况的最佳方法,如果出现错误,我希望代码跳到代码中的特定行或标签。我试过一些方法,但似乎有点问题。运行Excel时完全崩溃,或者忽略错误转到时的my 我尝试对4个不同的错误实例执行此操作,我考虑使用函数,但我不确定它们是否会执行我想要的操作,因为如果存在错误,并且没有错误,那么它将不会运行函数,从而完全跳过代码 我还应该提到的错误是,当筛选条件为空时,单元格中没有任何内容,我的“Add break”代码错误表明没有单元格可供检查 如有任何建议或帮助,将不胜感激 谢

我试图找出处理可能出现错误弹出的情况的最佳方法,如果出现错误,我希望代码跳到代码中的特定行或标签。我试过一些方法,但似乎有点问题。运行Excel时完全崩溃,或者忽略错误转到时的my

我尝试对4个不同的错误实例执行此操作,我考虑使用函数,但我不确定它们是否会执行我想要的操作,因为如果存在错误,并且没有错误,那么它将不会运行函数,从而完全跳过代码

我还应该提到的错误是,当筛选条件为空时,单元格中没有任何内容,我的“Add break”代码错误表明没有单元格可供检查

如有任何建议或帮助,将不胜感激

谢谢

以下是我的工作内容:

On Error GoTo ErrSkip1:
lr = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
ActiveSheet.Range("A1:O" & lr).AutoFilter Field:=12, Criteria1:="Item Merch Change"
ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Add(Range( _
"A2:A" & lr), xlSortOnFontColor, xlAscending, , xlSortNormal).SortOnValue.Color _
= RGB(153, 204, 0)
With ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

'Add Break
Set rng = Range("A2:A" & lr).SpecialCells(xlCellTypeVisible)
    irow = rng.Row
    icol = rng.Column
Do
If Cells(irow + 1, icol).Font.ColorIndex <> Cells(irow, icol).Font.ColorIndex Then
   Cells(irow + 1, icol).EntireRow.Insert shift:=xlDown
   Cells(irow + 1, icol).EntireRow.ClearFormats
   irow = irow + 2
Else
   irow = irow + 1
End If
'
Loop While Not Cells(irow, icol).Text = ""

ErrSkip1:
    Range("A2").Select
    ActiveSheet.ShowAllData

'Format and sort RD Changes
On Error GoTo ErrSkip2
lr = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
ActiveSheet.Range("A1:O" & lr).AutoFilter Field:=12, Criteria1:="RD Change"
ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Add(Range( _
"A2:A" & lr), xlSortOnFontColor, xlAscending, , xlSortNormal).SortOnValue.Color _
= RGB(153, 204, 0)
With ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

'Add Break
Set rng = Range("A2:A" & lr).SpecialCells(xlCellTypeVisible)
    irow = rng.Row
    icol = rng.Column
Do
If Cells(irow + 1, icol).Font.ColorIndex <> Cells(irow, icol).Font.ColorIndex Then
   Cells(irow + 1, icol).EntireRow.Insert shift:=xlDown
   Cells(irow + 1, icol).EntireRow.ClearFormats
   irow = irow + 2
Else
   irow = irow + 1
End If
'
Loop While Not Cells(irow, icol).Text = ""

ErrSkip2:
  Range("A2").Select
  ActiveSheet.ShowAllData
错误转到ErrSkip1:
lr=ActiveSheet.Cells(Rows.Count,1).End(xlUp).Row
ActiveSheet.Range(“A1:O”和lr)。自动筛选字段:=12,标准1:=“项目变更”
ActiveWorkbook.Worksheets(“Sheet1”).AutoFilter.Sort.SortFields.Add(范围(_
“A2:A”&lr),xlSortOnFontColor,xlSortNormal,xlSortNormal)。SortOnValue.Color_
=RGB(153、204、0)
使用ActiveWorkbook.Worksheets(“Sheet1”).AutoFilter.Sort
.Header=xlYes
.MatchCase=False
.方向=xlTopToBottom
.SortMethod=xl拼音
.申请
以
“加上休息
设置rng=范围(“A2:A”和lr)。特殊单元格(xlCellTypeVisible)
irow=rng.Row
icol=rng.列
做
如果单元格(irow+1,icol).Font.ColorIndex单元格(irow,icol).Font.ColorIndex则
单元格(irow+1,icol).EntireRow.Insert shift:=xlDown
单元格(irow+1,icol).EntireRow.ClearFormats
irow=irow+2
其他的
irow=irow+1
如果结束
'
非单元格时循环(irow,icol)。Text=“”
错误1:
范围(“A2”)。选择
ActiveSheet.ShowAllData
'格式和排序RD更改
关于错误转到ErrSkip2
lr=ActiveSheet.Cells(Rows.Count,1).End(xlUp).Row
ActiveSheet.Range(“A1:O”和lr)。自动筛选字段:=12,标准1:=“RD更改”
ActiveWorkbook.Worksheets(“Sheet1”).AutoFilter.Sort.SortFields.Add(范围(_
“A2:A”&lr),xlSortOnFontColor,xlSortNormal,xlSortNormal)。SortOnValue.Color_
=RGB(153、204、0)
使用ActiveWorkbook.Worksheets(“Sheet1”).AutoFilter.Sort
.Header=xlYes
.MatchCase=False
.方向=xlTopToBottom
.SortMethod=xl拼音
.申请
以
“加上休息
设置rng=范围(“A2:A”和lr)。特殊单元格(xlCellTypeVisible)
irow=rng.Row
icol=rng.列
做
如果单元格(irow+1,icol).Font.ColorIndex单元格(irow,icol).Font.ColorIndex则
单元格(irow+1,icol).EntireRow.Insert shift:=xlDown
单元格(irow+1,icol).EntireRow.ClearFormats
irow=irow+2
其他的
irow=irow+1
如果结束
'
非单元格时循环(irow,icol)。Text=“”
ErrSkip2:
范围(“A2”)。选择
ActiveSheet.ShowAllData

如果您的问题是这一行:

Set rng = Range("A2:A" & lr).SpecialCells(xlCellTypeVisible)
可以合理地预测可能会有过滤掉所有行的情况,然后最好执行以下操作:

Set rng = Nothing 'if rng may already have been assigned
On Error Resume Next
Set rng = Range("A2:A" & lr).SpecialCells(xlCellTypeVisible)
On Error Goto 0

If Not rng is nothing then
    'work with rng
Else
    'no visible rows...
End if
…如果这是一项常见任务,那么您可以通过将代码拆分为函数来简化代码

Function VisibleCells(rngIn as Range) As Range
    Dim rv As Range
    On Error Resume Next
    Set rng = rngIn.SpecialCells(xlCellTypeVisible)
    On Error Goto 0
    Set VisibleCells = rv
End Function

如果您的问题是这一行:

Set rng = Range("A2:A" & lr).SpecialCells(xlCellTypeVisible)
可以合理地预测可能会有过滤掉所有行的情况,然后最好执行以下操作:

Set rng = Nothing 'if rng may already have been assigned
On Error Resume Next
Set rng = Range("A2:A" & lr).SpecialCells(xlCellTypeVisible)
On Error Goto 0

If Not rng is nothing then
    'work with rng
Else
    'no visible rows...
End if
…如果这是一项常见任务,那么您可以通过将代码拆分为函数来简化代码

Function VisibleCells(rngIn as Range) As Range
    Dim rv As Range
    On Error Resume Next
    Set rng = rngIn.SpecialCells(xlCellTypeVisible)
    On Error Goto 0
    Set VisibleCells = rv
End Function

出错转到ErrSkip1
而不使用
注意:在第一个错误之后,Excel将无法捕获更多错误,直到它到达
恢复
行。您的过程做了太多事情,并且“快乐路径”与“错误路径”交织在一起-使用
退出子项
在“快乐路径”之后退出该过程,然后在下面编写错误处理代码:这确保错误处理代码只在错误状态下运行。在错误状态下运行的每个代码路径都需要恢复到下一个或特定标签;在错误状态下抛出的任何错误都将被取消处理,并使所有内容都付之一炬。请!这曾经被称为“意大利面代码”@Patrickonorez仍然是…
错误GoTo ErrSkip1
没有
注意:在第一个错误之后,Excel将无法捕获更多错误,直到它到达
恢复
行。您的过程做了太多事情,您的“快乐路径”与“错误路径”交织在一起-使用
Exit Sub
在“快乐路径”后退出过程,然后在下面对错误处理进行编码:这确保错误处理代码仅在错误状态下运行。在错误状态下运行的每个代码路径都需要恢复到下一个或特定标签;在错误状态下抛出的任何错误都将被取消处理,并使所有内容都付之一炬。请!这曾经被称为“意大利面代码”@patrickhornez仍然是…使用这个建议和函数使这个过程工作。再次感谢你的建议!使用此建议和功能使此过程正常工作。再次感谢你的建议!