简体   繁体   English

比较 A 列和 B 列的数据一次

[英]Comparing Data from Column A and Column B Once

I am currently in the process of analysing data from Excel, and would like to make comparisons between data in Column A and Column B, identifying duplicate data.我目前正在分析来自 Excel 的数据,并想在 A 列和 B 列中的数据之间进行比较,识别重复数据。 I am using the following code:我正在使用以下代码:

Sub Find_Matches()
Dim CompareRange As Variant, x As Variant, y As Variant
Set CompareRange = Range("c2", Range("c2").End(xlUp))

For Each x In Selection
    For Each y In CompareRange
        If x = y Then x.Offset(0, 1) = x
    Next y
Next x
End Sub

This code has been taken from MSDN, so if it finds a match in Column C against Column A, it will display the matched number in Column B. For the most part it does what I need.此代码取自 MSDN,因此如果它在 C 列中找到与 A 列匹配的代码,它将在 B 列中显示匹配的数字。在大多数情况下,它满足我的需求。 However I am looking to modify this code so it only matches a number in the list once.但是我希望修改此代码,以便它只匹配列表中的一个数字一次。

Example of what the code currently does:代码当前执行的示例:

A2  B2  C2
1   1   1
1   1   2
1   1   3

So essentially, because the number 1 appears once in Column C, Column A keeps finding a match.所以本质上,因为数字 1 在 C 列中出现了一次,所以 A 列一直在寻找匹配项。

What I would like it to do is:我希望它做的是:

A2  B2  C2
1   1   1
1       2
1       3

So because the number 1 only appears in Column C once, it should only be matched once against the numbers in Column A.所以因为数字 1 只在 C 列中出现一次,所以它应该只与 A 列中的数字匹配一次。

I'm assuming this is probably something simple, but I can't seem to determine the logic.我假设这可能很简单,但我似乎无法确定逻辑。 Could someone point me in the right direction please?有人能指出我正确的方向吗?

Testing for duplicates can be simple or complicated depending on how fast you want your procedure to be and how large the data sets are.重复测试可以简单也可以复杂,具体取决于您希望程序运行的速度和数据集的大小。

I personally favour the Collection object because it has a unique key and testing for the existence of that key is very fast, especially if the dataset is large.我个人更喜欢Collection对象,因为它有一个唯一的键,并且测试该键是否存在的速度非常快,尤其是在数据集很大的情况下。 The unique test is done by seeing if the code throws an error when you interrogate the Collection for a particular key.唯一性测试是通过查看当您查询特定键的Collection时代码是否抛出错误来完成的。 Some are philosophically opposed to testing for errors - I have to say that I'm one, so I actually prefer the Dictionary object but for a task this mundane, I won't go through the steps to reference that.有些人在哲学上反对测试错误 - 我不得不说我是一个人,所以我实际上更喜欢Dictionary对象,但对于这种平凡的任务,我不会通过步骤来引用它。

You'll also see that the code below works with arrays rather than cells on the worksheet itself - again, just a matter of personal taste because it's quicker.您还将看到下面的代码适用于数组而不是工作表本身的单元格 - 同样,这只是个人喜好问题,因为它更快。

Const SOURCE_COL As String = "A"
Const SOURCE_START_ROW As Long = 2
Const COMPARE_COL As String = "C"
Const COMPARE_START_ROW As Long = 2
Const OUTPUT_COL As String = "B"

Dim ws As Worksheet
Dim sourceValues As Variant
Dim compareValues As Variant
Dim outputValues() As Variant
Dim sourceIndex As Long
Dim compareIndex As Long
Dim uniques As Collection
Dim val As Variant
Dim key As String
Dim exists As Variant

Set ws = ThisWorkbook.Worksheets("Sheet1")
sourceValues = ws.Range(ws.Cells(SOURCE_START_ROW, SOURCE_COL), _
                        ws.Cells(Rows.Count, SOURCE_COL).End(xlUp)).Value2

compareValues = ws.Range(ws.Cells(COMPARE_START_ROW, COMPARE_COL), _
                         ws.Cells(Rows.Count, COMPARE_COL).End(xlUp)).Value2

Set uniques = New Collection
ReDim outputValues(1 To UBound(sourceValues, 1), 1 To 1)

For sourceIndex = 1 To UBound(sourceValues, 1)

    val = sourceValues(sourceIndex, 1)
    key = CStr(val)
    exists = Empty
    On Error Resume Next
    exists = uniques(key)
    On Error GoTo 0

    If IsEmpty(exists) Then

        For compareIndex = 1 To UBound(compareValues, 1)
            If val = compareValues(compareIndex, 1) Then
                outputValues(sourceIndex, 1) = val
                uniques.Add val, key
                Exit For
            End If
        Next

    End If

Next

ws.Cells(SOURCE_START_ROW, OUTPUT_COL).Resize(UBound(outputValues, 1)).Value = outputValues

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM