EXCEL VBA:比较两列,如果重复则删除一行

时间:2013-11-13 09:27:15

标签: excel excel-vba vba

我正在尝试将列C中的列表与从单元格Y1001开始的列表进行比较,然后在存在重复的位置,我想从列C中删除副本的整行。

然而,我的代码并没有产生这个结果! 我的错误在哪里?

谢谢!

Sub Button3_Click()

'run comparison'

'Turn off screen updating to speed up macro'
Application.ScreenUpdating = False

Dim iListCount As Long
Dim x As Variant
Dim iCtr As Long



' Get count of records to search through (list that will be deleted)'
iListCount = Sheets("Sheet1").Cells(Rows.Count, "C").End(xlUp).Row

' Loop through the "master" list'
For Each x In Sheets("Sheet1").Range("Y1001:Y" & Sheets("Sheet1").Cells(Rows.Count, "C").End(xlUp).Row)
   ' Loop through all records in the second list.
   For iCtr = iListCount To 1 Step -1
      ' Do comparison of next record'
      If x.Value = Sheets("Sheet1").Cells(iCtr, 1).Value Then
         ' If match is true then delete row.
         Sheets("Sheet1").Cells(iCtr, 1).EntireRow.Delete    
       End If
   Next iCtr
Next
Application.ScreenUpdating = True

End Sub

其他信息:

我得到“运行时错误424:需要对象”

当我运行调试时,错误行是:

If x.Value = Sheets("Sheet1").Cells(iCtr, 1).Value Then

但我没有发现我的错误。

1 个答案:

答案 0 :(得分:0)

快速扫描。
这段代码:

If x.Value = Sheets("Sheet1").Cells(iCtr, 1).Value Then

将您的YA的值进行比较,而不是C 应该是这样的。

If x.Value = Sheets("Sheet1").Cells(iCtr, 3).Value Then