Excel VBA 编程:错误处理与调试技巧
1. 错误处理技巧
1.1 运行时错误处理示例
下面是一个处理运行时错误的示例代码:
Sub SelectionSqrt()
Dim cell As Range
Dim ErrMsg As String
If TypeName(Selection) <> "Range" Then Exit Sub
On Error GoTo ErrorHandler
For Each cell In Selection
cell.Value = Sqr(cell.Value)
Next cell
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 5 'Negative number
Resume Next
Case 13 'Type mismatch
Resume Next
Case 1004 'Locked cell, protected sheet
MsgBox "Cell is locked. Try again.", vbCritical, cell.Address
Exit Sub
Case Else
ErrMsg = Error(Err.Number)
MsgBox "ERROR: " & ErrMsg, vbCritical, cell.Address
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



