使用VBA删除excel中的重复项

时间:2016-01-04 14:47:15

标签: excel vba excel-vba duplicates

我对VBA相当新,可以提供一些帮助。我已经在线查看,但我发现了一些代码,但无法修改我的需求。

我试图创建一个宏,让我看看它们是否是A列和B列之间的任何重复文本,如果A列中的文本与B列匹配,那么我们将需要删除整行。列在同一张纸上

我正在尝试创建一个可以执行此操作的循环。我还必须指出,列表的长度确实每周增加

我将不胜感激任何帮助

谢谢

2 个答案:

答案 0 :(得分:0)

您可以尝试使用代码VBA:

Sub DeleteRowWithContents()
'========================================================================
' DELETES ALL ROWS FROM A2 DOWNWARDS WITH THE WORDs "Record Only" IN COLUMN D
'========================================================================
    Last = Cells(Rows.Count, "D").End(xlUp).Row
    For i = Last To 1 Step -1
        If (Cells(i, "D").Value) = "Record Only" Then
    'Cells(i, "A").EntireRow.ClearContents ' USE THIS TO CLEAR CONTENTS BUT NOT DELETE ROW
            Cells(i, "A").EntireRow.Delete
        End If
    Next i
End Sub

您可以针对您的问题更新此代码! 当我需要从a2向下删除所有行时,我使用它,并在d列中使用“仅记录”字样。

答案 1 :(得分:0)

也许试试这个:

Sub DeleteRowWithContents()
    Dim ColumnAValue As String
    Dim ColumnBValue As String
    Dim xlWB As Worksheet

    Set xlWB = ActiveWorkbook.ActiveSheet 'If it isn't the active sheet use second row:
    'Set xlWB = ActiveWorkbook.Sheets("NameOfSheet") 'Change to the name of your sheet

    For i = 1 To EOF  'This goes through the whole document to the last row automatically, EOF means "End Of File"
        ColumnAValue = xlWB.Cells(i, 1).Value 'row i, column "a"
        ColumnBValue = xlWB.Cells(i, 2).Value 'row i, column "b"
        If (ColumnAValue = ColumnBValue) Then
            xlWB.Range(ColumnAValue).Select
            Selection.EntireRow.Delete 'NOTE!!
        End If
    Next i
End Sub

注意:我不太确定这是否有效,现在无法测试。如果没有,请尝试这样做:

EntireRow.Select
Selection.Delete