删除重复行。 (类型不匹配错误)

时间:2015-11-26 03:18:39

标签: excel vba excel-vba

我在Line &#34上遇到类型不匹配错误;如果不LRow =范围(" C65536")。结束(xlUp).Row ="&# 34;然后"

Private Sub DEDUPLICATE_Click()
Application.ScreenUpdating = False
Dim n As Long
Dim LRow As Long
    If Not LRow = Range("C65536").End(xlUp).Row = "" Then
        LRow = Range("C65536").End(xlUp).Row
        For n = LRow To 6 Step -1
            If Application.WorksheetFunction.CountIf(Range("C6:C" & n), Range("C" & n).Text) > 1 Then
            Range("C" & n).EntireRow.Delete
        End If
    Next n
End If

此代码应删除除空行之外的所有重复实体。试图将数据类型从Long更改为Variant但删除所有行,包括空行。

2 个答案:

答案 0 :(得分:1)

因为行计数是一个数值,你将它与字符串

进行比较
Private Sub DEDUPLICATE_Click()
    Application.ScreenUpdating = False
    Dim n As Long
    Dim LRow As Long
    If Not LRow = Range("C65536").End(xlUp).Row = 0 Then
        LRow = Range("C65536").End(xlUp).Row
        For n = LRow To 6 Step -1
            If Application.WorksheetFunction.CountIf(Range("C6:C" & n), Range("C" & n).Text) > 1 Then
                Range("C" & n).EntireRow.Delete
            End If
        Next n
    End If
End Sub

由于

答案 1 :(得分:1)

试试这个:

Private Sub DEDUPLICATE_Click()
Application.ScreenUpdating = False
Dim n As Long
Dim LRow As Long

    LRow = Range("C65536").End(xlUp).Row

    For n = LRow To 6 Step -1
        If Application.WorksheetFunction.CountIf(Range("C6:C" & n), Range("C" & n).Text) > 1 Then
        If Not Range("C" & n).Value = "" Then
            Range("C" & n).EntireRow.Delete
        End If
        End If
    Next n
End Sub