首页 > 解决方案 > 删除连续重复

问题描述

我想删除连续的重复但不是连续的空白行,但它也在删除连续的空白单元格。这是我的代码:

Sub Dupl()

    Dim i As Long
    Dim y As Long
    Dim ws As Worksheet

    Set ws = Worksheets("Sheet1")

    With ws
        For i = .Cells(.Rows.Count, 1).End(xlUp).Row To 3 Step -1
            If .Cells(i, 1).Value = .Cells(i - 1, 1).Value Then
                If .Cells(i, 1).Value = " " Then
                    GoTo y
                End If
                .Rows(i).Delete
y:
            End If
        Next i
    End With

End Sub

标签: excelvbarow

解决方案


您必须使用该IsEmpty()函数来检查单元格是否为空或包含值。空格被认为是一个值。

    For i = .Cells(.Rows.Count, 1).End(xlUp).Row To 3 Step -1
        If .Cells(i, 1).Value = .Cells(i - 1, 1).Value Then
            If Not IsEmpty(.Cells(i, 1).Value) Then
                .Rows(i).Delete
            End If
        End If
    Next i

推荐阅读