如何在excel-vba中解决错误400

时间:2022-05-05 15:06:10

I am writing an excel-vba to find and delete entire row if having blanks cell in a particular column. My macro is working fine if there is at least one blank cell but showing error 400 if there is no blank cell. My code is

我正在编写一个excel-vba来查找和删除整行,如果在特定列中清空单元格的话。如果至少有一个空单元格我的宏工作正常,但如果没有空单元格则显示错误400。我的代码是

Sub GPF_Sign()
Dim i As Integer, n as integer
Dim LROW As Long

    LROW = Sheets("GPF").Range("B200").End(xlUp).Row

    n = Range("D9:D" & LROW).SpecialCells(xlCellTypeBlanks).Cells.Count
    If n > 0 Then
        Range("D9:D" & LROW).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    End If
End Sub

3 个解决方案

#1


1  

Take your pick

随便挑选

Way 1: Using OERN (On Error Resume Next)

方式1:使用OERN(接下来错误恢复)

Sub WAY_ONE()
    Dim ws As Worksheet, LROW As Long
    Dim rng As Range

    Set ws = Sheets("GPF")

    With ws
        LROW = .Range("B" & .Rows.Count).End(xlUp).Row

        On Error Resume Next
        Set rng = .Range("D9:D" & LROW).SpecialCells(xlCellTypeBlanks)
        On Error GoTo 0

        If Not rng Is Nothing Then rng.EntireRow.Delete
    End With
End Sub

Way 2: Using Autofilter

方式2:使用自动过滤器

Sub WAY_TWO()
    Dim ws As Worksheet, LROW As Long
    Dim rng As Range

    Set ws = Sheets("GPF")

    With ws
        .AutoFilterMode = False

        LROW = .Range("B" & .Rows.Count).End(xlUp).Row

        Set rng = .Range("D9:D" & LROW)

        With rng 'Filter, offset(to exclude headers) and delete visible rows
            .AutoFilter Field:=1, Criteria1:=""
            .Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        End With

        .AutoFilterMode = False
    End With
End Sub

#2


4  

You can use On Error Resume Next, but this is not a usually recommended approach because it may mask other errors. Instead, try computing n in an error free way:

您可以使用On Error Resume Next,但这不是通常建议的方法,因为它可能会掩盖其他错误。相反,尝试以无错误的方式计算n:

n = Application.CountIf(Sheets("GPF").Range("D9:D" & LROW), "")

yet another, still better way is to use AutoFilter:

另一种更好的方法是使用AutoFilter:

Sub GPF_Sign()
  With Sheets("GPF").Range("D8:D200")
    .AutoFilter 1, ""
    .Offset(1).EntireRow.Delete
    .AutoFilter
  End With
End Sub

#3


1  

The method you are using ".SpecialCells(xlCellTypeBlanks)" is attempting to return a range. If there are no blank cells, the next part ".cells.count" is attempting to count Nothing. That's why it gives you an error.

您使用“.SpecialCells(xlCellTypeBlanks)”的方法正在尝试返回范围。如果没有空白单元格,则下一部分“.cells.count”正在尝试计算Nothing。这就是为什么它会给你一个错误。

Since you are already checking if n>0, you could just add On Error Resume Next right above the "n= " line. If there is more code after this, you probably want to put a On Error GoTo 0 after this part so it doesn't ignore later errors.

由于您已经在检查n> 0,因此您只需在“n =”行上方添加On Error Resume Next。如果此后有更多代码,您可能希望在此部分之后放置On Error GoTo 0,因此它不会忽略以后的错误。

#1


1  

Take your pick

随便挑选

Way 1: Using OERN (On Error Resume Next)

方式1:使用OERN(接下来错误恢复)

Sub WAY_ONE()
    Dim ws As Worksheet, LROW As Long
    Dim rng As Range

    Set ws = Sheets("GPF")

    With ws
        LROW = .Range("B" & .Rows.Count).End(xlUp).Row

        On Error Resume Next
        Set rng = .Range("D9:D" & LROW).SpecialCells(xlCellTypeBlanks)
        On Error GoTo 0

        If Not rng Is Nothing Then rng.EntireRow.Delete
    End With
End Sub

Way 2: Using Autofilter

方式2:使用自动过滤器

Sub WAY_TWO()
    Dim ws As Worksheet, LROW As Long
    Dim rng As Range

    Set ws = Sheets("GPF")

    With ws
        .AutoFilterMode = False

        LROW = .Range("B" & .Rows.Count).End(xlUp).Row

        Set rng = .Range("D9:D" & LROW)

        With rng 'Filter, offset(to exclude headers) and delete visible rows
            .AutoFilter Field:=1, Criteria1:=""
            .Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        End With

        .AutoFilterMode = False
    End With
End Sub

#2


4  

You can use On Error Resume Next, but this is not a usually recommended approach because it may mask other errors. Instead, try computing n in an error free way:

您可以使用On Error Resume Next,但这不是通常建议的方法,因为它可能会掩盖其他错误。相反,尝试以无错误的方式计算n:

n = Application.CountIf(Sheets("GPF").Range("D9:D" & LROW), "")

yet another, still better way is to use AutoFilter:

另一种更好的方法是使用AutoFilter:

Sub GPF_Sign()
  With Sheets("GPF").Range("D8:D200")
    .AutoFilter 1, ""
    .Offset(1).EntireRow.Delete
    .AutoFilter
  End With
End Sub

#3


1  

The method you are using ".SpecialCells(xlCellTypeBlanks)" is attempting to return a range. If there are no blank cells, the next part ".cells.count" is attempting to count Nothing. That's why it gives you an error.

您使用“.SpecialCells(xlCellTypeBlanks)”的方法正在尝试返回范围。如果没有空白单元格,则下一部分“.cells.count”正在尝试计算Nothing。这就是为什么它会给你一个错误。

Since you are already checking if n>0, you could just add On Error Resume Next right above the "n= " line. If there is more code after this, you probably want to put a On Error GoTo 0 after this part so it doesn't ignore later errors.

由于您已经在检查n> 0,因此您只需在“n =”行上方添加On Error Resume Next。如果此后有更多代码,您可能希望在此部分之后放置On Error GoTo 0,因此它不会忽略以后的错误。