运行VBA代码以根据特定条件删除行时,键入不匹配错误

我试图根据一些特定的标准删除Excel工作表中的一些行。 在某些时候,它给出了以下types不匹配错误:

SS

据我所知,从部分创build的工作表,错误发生在DO循环在

Or Len(Cells(r, 1)) = 5 _ Or Int(Right(Cells(r, 1), 4)) > 4000 _ Or Cells(r, 3) = 0 

部分。 我应该怎么做来纠正错误? 另外,如果你能推荐我一个更快的运行代码的改进,我真的很感激它。 完整的代码如下:

 Sub delrows() Dim r, RowCount As Long r = 2 ActiveSheet.Columns(1).Select RowCount = UsedRange.Rows.Count userresponse = MsgBox("You have " & RowCount & " rows", vbOKOnly, "Info") Rows(RowCount).Delete Shift:=xlUp ' Trim spaces Columns("A:A").Select Selection.Replace What:=" ", Replacement:="", LookAt:=xlPart, _ SearchOrder:=xlByRows, MatchCase:=False, searchFormat:=False, _ ReplaceFormat:=False ' Delete surplus columns Range("L:T,V:AA,AE:AG,AR:AR,AU:AU,AZ:AZ").Select Selection.Delete Shift:=xlToLeft ' Delete surplus rows Do If Left(Cells(r, 1), 1) = "D" _ Or Left(Cells(r, 1), 1) = "H" _ Or Left(Cells(r, 1), 1) = "I" _ Or Left(Cells(r, 1), 2) = "MD" _ Or Left(Cells(r, 1), 2) = "ND" _ Or Left(Cells(r, 1), 3) = "MSF" _ Or Left(Cells(r, 1), 5) = "MSGZZ" _ Or Len(Cells(r, 1)) = 5 _ Or Int(Right(Cells(r, 1), 4)) > 4000 _ Or Cells(r, 3) = 0 Then Rows(r).Delete Shift:=xlUp Else: r = r + 1 End If Loop Until (r = RowCount) End Sub 

条件Or Int(Right(Cells(r, 1), 4))是一个问题,因为它假设它所评估的单元格内容的部分是一个数字。

如果它不是一个数字,那么Int()函数会抛出你所看到的那种types不匹配错误。

如果您在应用Int()函数之前先testing它是否实际上是一个数字,那将会更好。 您可以使用IsNumeric()函数来执行此操作。