VBA:方法'价值'对象'范围'失败

时间:2017-12-09 22:17:21

标签: excel vba

我已经尝试在与该主题相关的其他答案中找到该主题的解决方案,但似乎没有任何效果。

我尝试使用嵌套循环删除重复项。数据由57241行和21列组成,我需要的标准只是前8列。我的目标是删除整个行,如果这8列中的任何一行与另一行匹配。此外,我是VBA的初学者,我不确定我所做的一切是否正确。

每次我尝试运行宏excel崩溃并向我显示运行时错误。

Sub remove_duplicates()

Dim lastRow As Long
Dim counter As Long
Dim r As Long

Application.ScreenUpdating = False      ' Stop screen flickering

lastRow = ActiveSheet.UsedRange.Rows.Count + ActiveSheet.UsedRange.Rows(1).Row - 1
r = 1
y = r + 1

Do While r < lastRow
    Do While y <= lastRow
        If (Cells(r, 1).Value = Cells(y, 1) _
            And Cells(r, 2).Value = Cells(y, 2) _
            And Cells(r, 3).Value = Cells(y, 3) _
            And Cells(r, 4).Value = Cells(y, 4) _
            And Cells(r, 5).Value = Cells(y, 5) _
            And Cells(r, 6).Value = Cells(y, 6) _
            And Cells(r, 7).Value = Cells(y, 7) _
            And Cells(r, 8).Value = Cells(y, 8) _
            And Cells(r, 9).Value = Cells(y, 9)) Then
            Rows(y).EntireRow.Delete
            counter = counter + 1
        Else
            y = y + 1
        End If

    Loop
    r = r + 1
    y = r + 1
Loop


MsgBox counter


End Sub

0 个答案:

没有答案