基于多列删除重复行

发布于 2025-02-14 00:03:44 字数 534 浏览 0 评论 0 原文

从基础上讲,我找到了以下公式,这是完美的,除了它仅过滤基于A列重复,而我只希望删除行A,B和C,而所有的行都被重复。

Sub removeDupes()
    Dim i As Long
    Dim ws As Worksheet

    Set ws = ActiveSheet 'This can be changed to a specific sheet: Worksheets("sheetName")

    With ws

        For i = .Cells(.Rows.Count, 1).End(xlUp).Row To 3 Step -1
            If .Cells(i, 1).Value = .Cells(i + 1, 1).Value Then
                .Rows(i).Delete
            End If
        Next i
    End With
End Sub

如何编辑此代码,以便适用于3列?

Bascially I have found the below formula which is perfect except it only filters duplicates out based on column A, whereas I only want the rows deleted if Col A, B and C are all duplicated.

Sub removeDupes()
    Dim i As Long
    Dim ws As Worksheet

    Set ws = ActiveSheet 'This can be changed to a specific sheet: Worksheets("sheetName")

    With ws

        For i = .Cells(.Rows.Count, 1).End(xlUp).Row To 3 Step -1
            If .Cells(i, 1).Value = .Cells(i + 1, 1).Value Then
                .Rows(i).Delete
            End If
        Next i
    End With
End Sub

How can I edit this code so it applies to 3 columns?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

且行且努力 2025-02-21 00:03:45

/ruf3q.jpg“ alt =”在此处输入图像描述”>

Option Explicit

Sub RemoveDupesShort()
    
    With ActiveSheet.UsedRange
        .Range("A3", .Cells(.Rows.Count, .Columns.Count)) _
            .RemoveDuplicates (VBA.Array(1, 2, 3))
    End With
    
    MsgBox "Duplicates removed.", vbInformation
    
End Sub

Sub RemoveDupes()
    
    ' Define constants.
    Const FirstCellAddress As String = "A3"
    Dim DupeCols() As Variant: DupeCols = VBA.Array("A", "B", "C")
    
    ' Reference the worksheet ('ws').
    Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
    ' For worksheet 'Sheet1' in the workbook containing this code, instead use:
    'Set ws = ThisWorkbook.Worksheets("Sheet1")
    
    ' Reference the range ('rg').
    Dim rg As Range
    With ws.UsedRange
        Set rg = ws.Range(FirstCellAddress, .Cells(.Rows.Count, .Columns.Count))
    End With
    
    ' Write the column numbers to a zero-based variant array ('Cols').
    
    Dim cUpper As Long: cUpper = UBound(DupeCols)
    Dim Cols() As Variant: ReDim Cols(0 To cUpper)
    
    Dim c As Long
    
    For c = 0 To cUpper
        Cols(c) = ws.Columns(DupeCols(c)).Column
    Next c
    
    ' Remove duplicates.
    rg.RemoveDuplicates (Cols)
    ' Note that the array of column numbers also needs to be evaluated:
    ' '(Cols)' which is short for 'Evaluate(Cols)'
    
    ' Inform.
    MsgBox "Duplicates removed.", vbInformation
    
End Sub

Remove Duplicates

Before

enter image description here

After

enter image description here

Option Explicit

Sub RemoveDupesShort()
    
    With ActiveSheet.UsedRange
        .Range("A3", .Cells(.Rows.Count, .Columns.Count)) _
            .RemoveDuplicates (VBA.Array(1, 2, 3))
    End With
    
    MsgBox "Duplicates removed.", vbInformation
    
End Sub

Sub RemoveDupes()
    
    ' Define constants.
    Const FirstCellAddress As String = "A3"
    Dim DupeCols() As Variant: DupeCols = VBA.Array("A", "B", "C")
    
    ' Reference the worksheet ('ws').
    Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
    ' For worksheet 'Sheet1' in the workbook containing this code, instead use:
    'Set ws = ThisWorkbook.Worksheets("Sheet1")
    
    ' Reference the range ('rg').
    Dim rg As Range
    With ws.UsedRange
        Set rg = ws.Range(FirstCellAddress, .Cells(.Rows.Count, .Columns.Count))
    End With
    
    ' Write the column numbers to a zero-based variant array ('Cols').
    
    Dim cUpper As Long: cUpper = UBound(DupeCols)
    Dim Cols() As Variant: ReDim Cols(0 To cUpper)
    
    Dim c As Long
    
    For c = 0 To cUpper
        Cols(c) = ws.Columns(DupeCols(c)).Column
    Next c
    
    ' Remove duplicates.
    rg.RemoveDuplicates (Cols)
    ' Note that the array of column numbers also needs to be evaluated:
    ' '(Cols)' which is short for 'Evaluate(Cols)'
    
    ' Inform.
    MsgBox "Duplicates removed.", vbInformation
    
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文