我遇到了一些宏代码的问题,我从我在网上找到的一些代码改编了一些代码,并想知道是否有人能够提供帮助。比较两个不同的工作表中的两个单元格并删除任何重复的内容
本质上,我希望宏运行并将“工作列表”工作表中的两个单元格与“导入此处”工作表中的条目进行比较,并删除任何重复项。
当我运行代码时,它似乎在标题单元格上工作,但似乎没有工作。
任何帮助将不胜感激。
这里是下面的代码:我也尝试了自己的理解注释。
Sub Comparison_Macro()
Dim iListCount As Integer
Dim iCtr As Integer
' Turn off screen updating to speed up macro.
Application.ScreenUpdating = False
' Get count of records to search through (list that will be deleted).
iListCount = Sheets("Import Here").Range("A1:A1000").Rows.Count
' Loop through the "master" list.
For Each x In Sheets("Working List").Range("A1:A30")
' Loop through all records in the second list.
For iCtr = 1 To iListCount
' Do comparison of Column A in next record.
If x.Value = Sheets("Import Here").Cells(iCtr, 1).Value Then
'Do comparison of Column B in next record.
If Sheets("Working List").Cells(iCtr, 2) = Sheets("Import Here").Cells(iCtr, 2).Value Then
' If match is true for Columns A and B then delete row.
Sheets("Import Here").Cells(iCtr, 1).EntireRow.Delete xlShiftUp
End If
' Increment counter to account for deleted row.
iCtr = iCtr + 1
End If
Next iCtr
Next
Application.ScreenUpdating = True
MsgBox "Done!"
End Sub
你有一个评论,说“增量专柜就占删除行,但就是if语句,其中该行被删除,所以它会增加外即使一行没有被删除。当你删除一行时,我认为你必须从iCtrl -1,但无论你做什么都会导致你在每张表上检查的行变得不同步,然后你将不会得到任何进一步的匹配。了解工作表是否包含重复数据的最佳方法是在代码中或工作表上的备用列中使用countif。 – Gordon