删除空行宏时键入不匹配

时间:2019-07-08 20:51:15

标签: excel vba

我创建了一个宏,以从所有工作表中删除所有空行。宏运行,但出现“类型不匹配”错误。为什么会出现此错误? 谢谢!

Sub RemoveEmptyRows()

Dim I As Integer, row As Long
Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets

    For row = 10 To 1 Step -1
        If ws.Cells(row, 4).Value = "" Then ws.rows(row).Delete
    Next row
Next ws

End Sub

1 个答案:

答案 0 :(得分:0)

发生这种情况是由于.Value = "",并且单元格的值是错误的(它返回类型不匹配)。这是一个很好的解决方法:

Sub RemoveEmptyRows()

    Dim i As Long, row As Long
    Dim ws As Worksheet

    For Each ws In ThisWorkbook.Worksheets
        For row = 10 To 1 Step -1
            If IsError(ws.Cells(row, 4)) Then
                ws.Rows(row).Delete
            ElseIf ws.Cells(row, 4).Value = "" Then
                ws.Rows(row).Delete
            End If
        Next row
    Next ws

End Sub