2017-03-06 68 views
0

我遇到了一些宏代码的问题,我从我在网上找到的一些代码改编了一些代码,并想知道是否有人能够提供帮助。比较两个不同的工作表中的两个单元格并删除任何重复的内容

本质上,我希望宏运行并将“工作列表”工作表中的两个单元格与“导入此处”工作表中的条目进行比较,并删除任何重复项。

当我运行代码时,它似乎在标题单元格上工作,但似乎没有工作。

任何帮助将不胜感激。

这里是下面的代码:我也尝试了自己的理解注释。

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 
+0

你有一个评论,说“增量专柜就占删除行,但就是if语句,其中该行被删除,所以它会增加外即使一行没有被删除。当你删除一行时,我认为你必须从iCtrl -1,但无论你做什么都会导致你在每张表上检查的行变得不同步,然后你将不会得到任何进一步的匹配。了解工作表是否包含重复数据的最佳方法是在代码中或工作表上的备用列中使用countif。 – Gordon

回答

0

以下是使用countifs检查“工作列表”工作表上是否存在“导入此处”的列A和B的版本。由于它是从“Import Here”表单中删除行,所以代码在每行中循环并在“Working List”表单中找到时删除。

我的评论并不完全正确,因为我没有看到您在一张纸上为每一行循环读取另一张纸上的每一行,所以它可能没有失去同步。这说我仍然认为使用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 
0

您coudl考虑一个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 
+0

@ShaunJones,你通过它了吗? – user3598756

相关问题