Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 基于特定条件运行VBA代码删除行时出现类型不匹配错误_Loops_If Statement_Excel_Type Mismatch_Vba - Fatal编程技术网

Loops 基于特定条件运行VBA代码删除行时出现类型不匹配错误

Loops 基于特定条件运行VBA代码删除行时出现类型不匹配错误,loops,if-statement,excel,type-mismatch,vba,Loops,If Statement,Excel,Type Mismatch,Vba,我正试图根据某些特定条件删除Excel工作表中的某些行。在某些情况下,会出现以下类型不匹配错误: 据我从部分创建的工作表中了解,错误发生在 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

我正试图根据某些特定条件删除Excel工作表中的某些行。在某些情况下,会出现以下类型不匹配错误:

据我从部分创建的工作表中了解,错误发生在

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
条件
或Int(右(单元格(r,1,4))
是一个问题,因为它假定正在计算的单元格内容部分是一个数字

如果结果不是数字,那么
Int()
函数将抛出您看到的类型不匹配错误

在应用
Int()
函数之前,最好先测试它是否实际上是一个数字。您可以使用
IsNumeric()
函数来执行此操作。

条件
或Int(右(单元格(r,1,4))
是一个问题,因为它假定正在计算的单元格内容部分是一个数字

如果结果不是数字,那么
Int()
函数将抛出您看到的类型不匹配错误

在应用
Int()
函数之前,最好先测试它是否实际上是一个数字。您可以使用
IsNumeric()
函数来执行此操作