【问题标题】:VBA Excel - How to remove duplicate based in two columnsVBA Excel - 如何删除基于两列的重复项
【发布时间】:2016-03-23 01:28:42
【问题描述】:

Excel Example as I can't post pictures

我上传了一张带有一些示例的 excel 图片。 我需要的是:

  • 第 3 行和第 4 行重复,但第 4 行在“Dir”列中有数据。在这种情况下,删除两者
  • 第 12 行和第 13 行重复,“Dir”列中没有数据。在这种情况下,只留下一行

我使用条件格式突出显示重复项并使用“IF”“Countif”....但没有运气,也不知道如何完成此操作。

【问题讨论】:

  • Aaaa 到目前为止你尝试过什么?
  • 这可能会有所帮助:stackoverflow.com/help/how-to-ask
  • 我只有 'ActiveSheet.Range("A1:C" & K).RemoveDuplicates Columns:=1, Header:=xlYes' K 是一个字符串
  • 以下数组公式,返回重复项的行号=SMALL(IF((NOT(ISBLANK($C$2:$C$13))*(COUNTIFS($A$2:$A$13 ,$A$2:$A$13)>1)*(COUNTIFS($B$2:$B$13,$B$2:$B$13)>1)),ROW($A$2:$A$13)),ROWS ($G$1:$G1)) 你可以使用这个结果来显示重复的行...
  • 对不起,这个=SMALL(IF((NOT(ISBLANK($C$2:$C$13))*(COUNTIFS($A$2:$A$13,$A$2:$A$13 ,$B$2:$B$13,$B$2:$B$13)>1)),ROW($A$2:$A$13)),ROWS($G$1:$G1))

标签: excel vba


【解决方案1】:

对于基于公式的方法(使用辅助列,您可以执行以下操作:

  1. D2公式:=COUNTIFS($A$1:$A2,A2)
  2. E2公式:=COUNTIFS($A$2:$A$33,A2,$C$2:$C$33,"LD")
  3. F2公式:=IF(AND(COUNTIF($A$2:$A$21,A2)=1,E2=1),"Keep",IF(OR(AND(D2>0,E2>0),AND(D2>1,E2=0)),"Remove","Keep"))

向下拖动,然后过滤“删除”并删除可见单元格。

【讨论】:

  • 成功了。非常感谢斯科特。已经尝试了一些公式,但没有运气。你的工作很棒。
【解决方案2】:

如果你想要一个 VBA 选项,那么下面的代码就可以了:

  Option Explicit

Sub DeleteDupes()

    Dim workingRow As Long
    Dim workingItem As String
    Dim i As Long
    Dim occurances As Variant

    'Skip past header row
    workingRow = 1

    Do Until Range("A" & workingRow + 1).value = ""

        workingRow = workingRow + 1
        workingItem = Range("A" & workingRow).value

        If Len(Range("C" & workingRow)) = 0 Then

            occurances = FncGetOccurances(workingRow, workingItem)

            If IsArray(occurances) Then

                FncDeleteRange (occurances)
                workingRow = workingRow - UBound(occurances)

            End If

        End If

    Loop

End Sub

Private Function FncDeleteRange(value As Variant)

    Dim deleteRange As Range
    Dim i As Long

    For i = 1 To UBound(value)

            If i = 1 Then

                Set deleteRange = Range("A" & value(i), "C" & value(i))
                deleteRange.Select

            Else

                Union(deleteRange, Range("A" & value(i), "C" & value(i))).Select

            End If
    Next i

    Selection.Delete Shift:=xlUp

End Function

Private Function FncGetOccurances(masterIndex As Long, value As String) As Variant

    Dim rowsToReturn As Variant
    Dim i As Long
    Dim currentCell As Long
    Dim itemCount As Long

    i = 1

    Do While Range("A" & i + 1).value <> ""

        i = i + 1
        currentCell = Range("A" & i).value

        If currentCell = value And _
            i <> masterIndex And _
            Len(Range("C" & i)) <> 0 Then

            itemCount = itemCount + 1

            If itemCount = 1 Then

                ReDim rowsToReturn(itemCount)

            Else

                ReDim Preserve rowsToReturn(itemCount)

            End If

            rowsToReturn(itemCount) = i

        End If

    Loop

    FncGetOccurances = rowsToReturn

End Function

【讨论】:

  • 谢谢戴维,但是当我运行宏时它给出了一个错误 "currentCell = Range("A" & i).value"
  • 错误是什么?你只是告诉我失败的线路吗?您的 Excel 工作表中有标题吗? Range("A" & i).value 失败时的值是多少(在“即时 Windows”中键入 DEBUG.PRINT RANGE("A" & i).VALUE
  • 错误是“运行时错误'6':溢出”,当我调试时它指向上面的行。它有标题。
  • 您正在运行的工作表中有多少行?谢谢。
  • 它是动态的。你可以有 40 到 400 行
猜你喜欢
  • 2022-12-15
  • 2017-04-05
  • 2018-03-07
  • 1970-01-01
  • 2012-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-04
相关资源
最近更新 更多