避免在Excel 2007中重复

时间:2018-04-08 07:51:11

标签: excel excel-formula duplicates excel-2007

我需要将数据从Sheet 1 Column A提取到Sheet 2 Column ASheet 2已在Column A中添加了一些值。因此,我需要一个公式来将数据从Sheet 1 Column A提取到Sheet 2。如果价值已经存在,则不需要复制,如果不需要提取数据。

例如:
第2页我写了一个公式:如果ID已经存在Sheet 1,那么会将标记添加到现有的标记中。
img1

第1页需要编写如下公式:如果IDSheet 2的新内容,则会将ID添加到带有标记的下一行。<登记/> img2

每两周将ID和标记添加到Sheet 1。所以,我需要Sheet 2成为一个整合的。在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

尝试

Option Explicit

Public Sub CopyUniqueOnly()

    Dim currCell As Range, dict As Object
    Set dict = CreateObject("Scripting.Dictionary")

    With ThisWorkbook.Worksheets("Sheet2")
        For Each currCell In .Range("A1", .Cells(.Rows.count, 1).End(xlUp))
            If Not dict.exists(currCell.Value) And Not IsEmpty(currCell) Then
                dict.Add currCell.Value, currCell.Value
            End If
        Next currCell
    End With

    Dim unionRng As Range

    With ThisWorkbook.Worksheets("Sheet1")
        Dim rng As Range
        For Each rng In .Range("A1", .Cells(.Rows.count, 1).End(xlUp))
            If Not dict.exists(rng.Value) And Not IsEmpty(rng) Then
                If Not unionRng Is Nothing Then
                    Set unionRng = Union(rng, unionRng)
                Else
                    Set unionRng = rng
                End If
            End If
        Next rng
    End With

    With ThisWorkbook.Worksheets("Sheet2")
        If Not unionRng Is Nothing Then unionRng.EntireRow.Copy .Cells(.Rows.count, 1).End(xlUp).Offset(1, 0)
    End With

End Sub

或者

将所有数据从表1复制到表2,然后只使用内置数据&gt;删除重复项并指定A列

Remove duplicates