VB on error goto
时间: 2024-05-16 18:12:36 浏览: 18
`On Error Goto` 是 Visual Basic 中的一个错误处理语句。它的作用是在程序发生错误时,跳转到指定的错误处理程序。语法为:
```
On Error Goto {label|0|1}
```
其中 `label` 是一个指定错误处理程序的标签,`0` 表示不处理错误,`1` 表示将错误信息存储到 `Err` 对象中。
当程序发生错误时,会根据 `On Error Goto` 语句指定的方式进行处理。如果指定了标签,程序会跳转到该标签指定的错误处理程序;如果指定了 `0`,则不进行任何处理;如果指定了 `1`,则将错误信息存储到 `Err` 对象中。
相关问题
VB on error goto 例句
以下是一个简单的 VB 代码示例,展示了如何使用 On Error GoTo 语句处理运行时异常:
```
Sub Example()
Dim x As Integer
On Error GoTo ErrorHandler
x = 1 / 0 ' 引发除以零的异常
MsgBox ("The result is " & x)
Exit Sub
ErrorHandler:
MsgBox ("An error occurred: " & Err.Description)
End Sub
```
在上面的示例中,当代码执行到 `x = 1 / 0` 这一行时,会引发一个除以零的异常。然后,代码会跳转到标签为 "ErrorHandler" 的代码块中,显示一个错误消息框。如果没有发生异常,代码将继续执行,并在消息框中显示计算结果。
On Error GoTo skip1 Application.ActiveProtectedViewWindow.Edit skip1: Cells.Select Cells.EntireColumn.AutoFit ActiveWorkbook.Save Range("A1").Select On Error GoTo skip2 Cells.Find(What:="期货持仓汇总", After:=ActiveCell, LookIn:=xlFormulas, LookAt _ :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ False, SearchFormat:=False).Activate i = Selection.Row + 2 i0 = i Do While Range("A" & i) <> "合计" i = i + 1 Loop i = i - 1 Range("A" & i0, "J" & i).Copy Workbooks("场内部期权每日结算.xlsm").Activate Sheets("当日持仓").Select Range("I2").Select Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False '贴期权持仓 Workbooks("011381506002_" & CStr(Format(now, "yyyy-mm-dd")) & ".xls").Activate skip2: Cells.Find(What:="期权持仓汇总", After:=ActiveCell, LookIn:=xlFormulas, LookAt _ :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ False, SearchFormat:=False).Activate为什么这段代码中on error goto skip2失效
在你提供的代码中,`On Error GoTo skip2` 失效的原因可能是因为 `Find` 方法的错误没有被正确捕获。`Find` 方法在找不到指定的值时会引发错误,但是在 `On Error GoTo skip2` 的下一行就立即使用了 `Activate` 方法,这可能导致错误被忽略而不会跳转到 `skip2` 标签。
为了确保错误被正确捕获和处理,你可以将 `Activate` 方法移到错误处理代码块内,例如:
```vb
On Error GoTo skip2
Cells.Find(What:="期货持仓汇总", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If Not ActiveCell Is Nothing Then
ActiveCell.Activate
' 在这里继续处理其他操作
i = Selection.Row + 2
i0 = i
Do While Range("A" & i) <> "合计"
i = i + 1
Loop
i = i - 1
Range("A" & i0, "J" & i).Copy
Workbooks("场内部期权每日结算.xlsm").Activate
Sheets("当日持仓").Select
Range("I2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
' 贴期权持仓
Workbooks("011381506002_" & CStr(Format(now, "yyyy-mm-dd")) & ".xls").Activate
skip2:
Cells.Find(What:="期权持仓汇总", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If Not ActiveCell Is Nothing Then
ActiveCell.Activate
' 在这里继续处理其他操作
End If
```
在上述修改后的代码中,我们将 `Activate` 方法放入了 `If Not ActiveCell Is Nothing` 的条件语句内,这样当 `Find` 方法找到了指定的值时才会执行后续的操作。如果没有找到,错误处理代码块将会执行,然后程序会继续执行下面的代码。这样可以确保错误被正确捕获,并且 `On Error GoTo skip2` 生效。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)