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

时间:2011-11-13 05:58:18

标签: excel vba excel-vba

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

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

1 个答案:

答案 0 :(得分:2)

使用On Error Goto <Label>

典型用途是

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