[英]Vba excel blank cells error handling
Sub Remove_Deregistered()
With Worksheets("Sheet2")
For Each cel In .Range(.Range("D2"), .Range("D2").End(xlDown))
If cel.Value Like "*Deregistered*" Then
Worksheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Offset(1).Value = cel.Offset(, 10).Value
cel.Resize(1, 1).ClearContents
Worksheets("Sheet3").Range("B" & Rows.Count).End(xlUp).Offset(1).Value = cel.Offset(, 11).Value
cel.Resize(1, 1).ClearContents
End If
Next
Sheet2.Range(.Range("D2"), .Range("G2").End(xlDown).Offset(, -3)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End With
End Sub
In the Sub above when the Range D2 doesnt have blank cells I am getting error in this line : 在上面的Sub中,当范围D2没有空白单元格时,我在此行中遇到错误:
Sheet2.Range(.Range("D2"), .Range("G2").End(xlDown).Offset(, -3)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
How can I prevent this error? 如何防止此错误?
You could use some error-handling code, eg: 您可以使用一些错误处理代码,例如:
BeginTry1:
On Error GoTo ErrHandler1
Worksheets("Sheet1").Range(.Range("D2"), _
.Range("G2").End(xlDown).Offset(, -3)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
ErrHandler1:
Debug.Print Err.Description
or you can ignore the error like this 或者您可以忽略这样的错误
On Error Resume Next
' your code that might give error here
If Err.Number <> 0 Then Debug.Print Err.Description ' optional to print the error in the VBA Immediate window
On Error GoTo 0 ' optional to reset the error handling
http://www.cpearson.com/excel/errorhandling.htm http://www.cpearson.com/excel/errorhandling.htm
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.