VBA中的错误处理 - 接下来的错误恢复 [英] Error handling in VBA - on error resume next
问题描述
我是VBA的新人。
我有以下代码:
ErrNr = 0
For Rw = StRw To LsRw 'ToDo speed up with fromrow torow
If Len(ThisWorkbook.Sheets(TsSh).Cells(Rw, TsCl)) = 0 Then
ThisWorkbook.Sheets(TsSh).Cells(Rw, TsCl).Interior.ColorIndex = 46
ErrNr = ErrNr + 1
End If
我的问题是如果页面上有错误,我的代码在此之后没有运行。我认为解决方案应该是:
My problem is if there is an error on the page, my code is not running after that. I think the solution should be with:
On Error Resume Next
N = 1 / 0 ' cause an error
If Err.Number <> 0 Then
N = 1
End If
但我不知道如何在这里使用这个代码。
But I don't know how to use this code here.
推荐答案
我解释了在循环遍历单元格范围和检查值时如何处理常见工作表错误的要求。如果您尝试查看包含错误的单元格(例如#N / A,#DIV / 0 !, #VALUE !,等),您将获得以下内容:
I have interpreted your requirement as to how to handle common worksheet errors when looping through a range of cells and examining the values. If you attempt to look at a cell that contains an error (e.g. #N/A, #DIV/0!, #VALUE!, etc) you will get something like:
Runtime error '13':
Type mismatch.
可以使用VBA的 IsError function 。
Dim rw As Long
With ThisWorkbook.Sheets(TsSh)
For rw = StRw To LsRw
If IsError(.Cells(rw, 1)) Then
.Cells(rw, 1).Interior.ColorIndex = 10
ElseIf Not CBool(Len(.Cells(rw, 1).Value2)) Then
.Cells(rw, 1).Interior.ColorIndex = 46
End If
Next rw
End With
在上面,我正在捕获单元格发生错误并将其着色为绿色。请注意,我正在检查他们之前的错误 我检查零长度。
In the above, I am catching the cells with an error and coloring them green. Note that I am examining them for errors before I check for a zero length.
这篇关于VBA中的错误处理 - 接下来的错误恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!