【问题标题】:Scrolling through two columns and viewing the differences and remove duplicates滚动两列并查看差异并删除重复项
【发布时间】:2019-01-25 18:04:58
【问题描述】:

我使用下面的代码根据第二列从 xcol(第一个选定列)中删除重复项。使用 2 个 for 循环,我正在检查第 1 列中的 2 个单元格和第 2 列中的 2 个单元格是否相同,然后仅从第 1 列中删除重复的单元格。我的代码会删除所有数据,无论是否存在重复项。知道为什么吗?谢谢。

Sub RemoveDuplicates()
    Dim xRow As Long
    Dim xCol As Long
    Dim x2Row As Long
    Dim x2Col As Long
    Dim xrg As Range
    Dim xrg2 As Range
    Dim xl As Long
    Dim x2 As Long

    On Error Resume Next

    Set xrg = Application.InputBox("Select a range:", "Kutools for Excel", _
                                    ActiveWindow.RangeSelection.AddressLocal, , , , , 8)

    Set xrg2 = Application.InputBox("Select a range:", "Kutools for Excel", _
                                    ActiveWindow.RangeSelection.AddressLocal, , , , , 8)

    xRow = xrg.Rows.Count + xrg.Row - 1
    x2Row = xrg2.Rows.Count + xrg2.Row - 1
    xCol = xrg.Column
    x2Col = xrg2.Column
    'MsgBox xRow & ":" & xCol
    Application.ScreenUpdating = False

    For x2 = x2Row To 2 Step -1
        For xl = xRow To 2 Step -1
            If ((Cells(xl, Col) = Cells(xl - 1, xCol)) And (Cells(x2, x2Col) = Cells(x2 - 1, x2Col))) Then
                Cells(xl, xCol) = ""
            End If
        Next xl
    Next x2

    Application.ScreenUpdating = True
End Sub

一个例子:

之前:

Group  ID 
2010   16
2010   16
2010   15
2012   15

之后(应该如何)

Group  ID 
2010   16
2010  
2010   15
2012   15

【问题讨论】:

  • 删除On Error Resume Next。此行隐藏了 所有 错误消息,但错误仍然发生。如果您看不到错误,则无法修复它们,如果您不修复它们,您的代码将无法正常工作。在执行任何其他操作之前删除该行并修复您的错误。

标签: excel vba


【解决方案1】:

在您的“if”行中,将 Col 与 xCol 交换!
使用“Option Explicit”来避免此类错误!

For x2 = x2Row To 2 Step -1
    For xl = xRow To 2 Step -1
        If ((Cells(xl, Col) = ...

更正此错误后,您的代码会进行以下比较(我相信这不是您想要做的):

x2 xl   Compare 1   Compare2 
5  5    B5=B4       A5=A4
5  4    B4=B3       A5=A4
5  3    B3=B2       A5=A4
5  2    B2=B1       A5=A4
4  5    B5=B4       A4=A3   => DELETE
4  4    B4=B3       A4=A3
4  3    B3=B2       A4=A3   => DELETE
4  2    B2=B1       A4=A3
3  5    B5=B4       A3=A2
3  4    B4=B3       A3=A2
3  3    B3=B2       A3=A2
3  2    B2=B1       A3=A2
2  5    B5=B4       A2=A1
2  4    B4=B3       A2=A1
2  3    B3=B2       A2=A1
2  2    B2=B1       A2=A1

为了打印比较的地址,我添加了以下几行:

        If ((Cells(xl, xCol) = Cells(xl - 1, xCol)) And (Cells(x2, x2Col) = Cells(x2 - 1, x2Col))) Then
            Debug.Print x2; xl; Cells(xl, xCol).Address; "="; Cells(xl - 1, xCol).Address, Cells(x2, x2Col).Address; "="; Cells(x2 - 1, x2Col).Address; "=> DELETE"
            Cells(xl, xCol) = ""
        Else
            Debug.Print x2; xl; Cells(xl, xCol).Address; "="; Cells(xl - 1, xCol).Address, Cells(x2, x2Col).Address; "="; Cells(x2 - 1, x2Col).Address
        End If

【讨论】:

  • 谢谢。是的,这不是我想做的。我的代码比较所有行的所有值,但我想要比较 2 个单元格(例如,A5=A4 .. B 相同),如果有重复项,则从单元格 x1 中删除值,然后“继续忘记这些值”(比较接下来的两个)
  • 只要把上表改正就可以了。然后你就有了你想要的引用,你就完成了!
猜你喜欢
  • 2018-01-12
  • 2017-02-09
  • 2012-12-29
  • 2017-06-05
  • 2010-11-22
  • 2015-11-29
  • 2016-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多