简体   繁体   English

比较两个不同工作表中的两个单元格并删除任何重复项

[英]Compare two cells in two different worksheets and delete any duplicates

I am having a few problems with some Macro code I have adapted from some code I found online and wondered if anyone would be able to help. 我从网上找到的一些代码改编而成的某些宏代码遇到一些问题,想知道是否有人可以提供帮助。

Essentially, I want the Macro to run and compare two cells in the 'Working List' worksheet to the entries in the 'Import Here' worksheet and delete any duplicates. 本质上,我希望宏运行并将“工作列表”工作表中的两个单元格与“此处输入”工作表中的条目进行比较,并删除所有重复项。

When I run the code, it seems to work on the title cell but then doesn't seem to work. 当我运行代码时,它似乎可以在标题单元格上工作,但随后似乎不起作用。

Any help would be greatly appreciated. 任何帮助将不胜感激。

Here is the code below: I have tried to annotate it for my own understanding too. 这是下面的代码:为了我自己的理解,我也尝试对其进行注释。

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

Here's a version that uses countifs to check if columns A and B on "Import Here" exist on the "Working List" sheet. 这是一个使用Countifs的版本来检查“工作清单”表上是否存在“在此处导入”上的列A和列B。 As it is deleting rows from the "Import Here" sheet the code loops through every row and deletes if it's found on the "Working List" sheet. 当它从“在此处导入”工作表中删除行时,代码循环遍历每一行,并删除在“工作列表”工作表中找到的行。

My comment was not entirely correct as I hadn't seen that you were looping through every row on one sheet for each row on the other so it maybe wasn't getting out of sync. 我的评论并不完全正确,因为我没有看到您在一张纸上的每一行之间循环浏览另一行上的每一行,因此它可能并没有保持同步。 That said I still think that using countifs is a better way to do this. 话虽如此,我仍然认为使用Countifs是一种更好的方法。

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 iCtr = 1 To iListCount
       ' Loop through all records in the second list.

      ' Do comparison of Column A and B in next record.

        If Application.WorksheetFunction.CountIfs(Range("'Working List'!A1:A1000"), Range("A" & iCtr), Range("'Working List'!B1:B1000"), Range("B" & iCtr)) > 0 Then
          Sheets("Import Here").Cells(iCtr, 1).EntireRow.Delete xlShiftUp
          iCtr = iCtr - 1
        End If

    Next iCtr
    Application.ScreenUpdating = True
    MsgBox "Done!"
End Sub

you coudl consider an Autofilter() approach: 您可以考虑使用Autofilter()方法:

Sub Comparison_Macro()
    Dim workingRng  As Range, importRng As Range, deleteRng As Range, cell As Range

    With Worksheets("Working List") '<--| reference "Working List" sheet
        Set workingRng = .Range("A1", .cells(.Rows.Count, 1).End(xlUp)) '<--| set the "Working List" sheet column A values from row 1 down to last not empty row to be checked in "Import Here" sheet
    End With

    With Sheets("Import Here") '<--| reference "Import Here" sheet
        With .Range("A1", .cells(.Rows.Count, 1).End(xlUp)) '<--| reference its column A range from row 1 down to last not empty row
            .AutoFilter Field:=1, Criteria1:=Application.Transpose(workingRng.Value), Operator:=xlFilterValues '<--| filter referenced cells with 'workingRng' values
            Set importRng = .SpecialCells(xlCellTypeVisible) '<--| set filtered cells to 'importRng' range
            Set deleteRng = .Offset(, 1).Resize(1, 1) '<--| initialize 'deleteRng' to a "dummy" cell that's out of range of interest: it'll be used to avoid subsequent checking against "nothing" before calling 'Union()' method and eventually discharged
        End With
        .AutoFilterMode = False
    End With

    For Each cell In importRng '<--| loop through filtered cells in "Import Here"
        If workingRng.Find(What:=cell.Value, LookIn:=xlValues, LookAt:=xlWhole).Offset(, 1) = cell.Offset(, 1) Then Set deleteRng = Union(deleteRng, cell) '<--| if current cell adjacent value matches corresponding value in "working range" then update 'deletRng'
    Next
    Set deleteRng = Intersect(importRng, deleteRng) '<--| get rid of "dummy" cell
    If Not deleteRng Is Nothing Then deleteRng.EntireRow.Delete '<--| if any survived cell in "Import Here" then delete corresponding rows
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在两个不同的Excel工作表中比较两个单元格区域? - How to compare two range of cells in two different excel Worksheets? 我想比较Excel中不同工作表中的两个列表以查找任何重复项 - I want to compare two lists in different worksheets in Excel to locate any duplicates 比较不同工作表中的两个单元格,将其他单元格设置为确定 - Compare two cells in different worksheets, set different cell as ok 使用for循环和数组比较来自不同工作表的两个单元格 - Compare two cells from different worksheets using for loop and array 比较两个不同工作表中的两个单元格 - Comparing two Cells in two different Worksheets 在不同的工作表上突出显示两个范围之间的重复 - Highlight Duplicates between two ranges on different worksheets 在两个不同的工作表中构造一对镜像单元 - Construct a pair of mirrored cells in two different worksheets 比较两个工作表并使用两者的重复项构建新工作表 - Compare two worksheets and built new with duplicates from both 在两个工作表中比较时间范围,并用颜色填充范围之间的单元格 - Compare range of time in two worksheets and fill the cells between ranges with a color Excel VBA比较两个工作表并将某些单元格输出到新的工作表 - Excel VBA Compare two worksheets and output certain cells to a new one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM