vba 如何在excel宏中的代码块之间跳转?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8109937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 12:10:05  来源:igfitidea点击:

How to jump between block of code in excel macro?

excelvbaexcel-vba

提问by kinkajou

LongitudeValue:
         'Longitude Value
          C = C + 1
          SrcWkb.Worksheets("sheet1").Range("N8:P8").Copy
            DstWks1.Cells(R, C).Select
             Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

FarEndSiteName:
           'Farend SIte Name
            C = C + 1
           SrcWkb.Worksheets("Sheet1").Range("A").Copy
            DstWks1.Cells(R, C).Select
             Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

Is it possible to jump between between two block of code when error occours in LongitudeValueblock of code so that code will resume from FarEndSiteName?

当代码块中出现错误时,是否可以在两个代码块之间跳转,LongitudeValue以便代码可以从其中恢复FarEndSiteName

采纳答案by chris neilsen

Use On Error Goto <Label>

On Error Goto <Label>

Typical use is

典型用途是

Sub MySub()
    Dim ...
    On Error Goto EH
    ' code...

CleanUp:
    On Error Resume Next
    ' Tidy up code...

Exit Sub
EH:
    If Err.Number = <some particular error> Then
        '  error handling code

        Resume  ' or Resume Next
    End If

    GoTo CleanUp

End Sub