【问题标题】:Find duplicate values and move to different sheet查找重复值并移动到不同的工作表
【发布时间】:2023-03-06 16:46:01
【问题描述】:

我有两列如下:

4   10
20  5
20  20
70  20
60  50
80  70
5   90
20  60
100

我需要一个宏来查找重复的对并将它们移动到单独的工作表中,以便当前工作表看起来像这样:

4   10
20  50
80  90
100

第 2 页看起来像这样:

20  20
20  20
70  70
5   5
60  60

我到处搜索,找不到解决问题的方法。到目前为止,我尝试过的所有代码和公式要么移动所有20,而不是只移动两对(因为两列中只有两对),要么保持原样。

我每天要整理大约 300 个条目,而且每天都会完全变化。对我的问题的任何帮助或指导将不胜感激。

我怎样才能达到所示的效果?

【问题讨论】:

  • 我不明白您需要在表 1 和表 2 中做什么,因为您需要按列删除重复项?还是按行?还是两者兼有?请你说的更清楚些。
  • ...我无法理解您如何获得进入表格 1 和 2 的值...我看不出这些值是如何重复/唯一的 - 例如,为什么 20 在表 1??

标签: vba excel duplicates excel-2007


【解决方案1】:

有很多方法可以做到这一点。这是一个例子。

试试这个。我已经对代码进行了注释,因此您理解它不会有问题。

Option Explicit

Sub Sample()
    Dim wsMain As Worksheet, wsOutput As Worksheet
    Dim lRowColA As Long, lRowColB As Long, i As Long, j As Long
    Dim aCell As Range, ColARng As Range, ColBRng As Range

    '~~> Set input Sheet and output sheet
    Set wsMain = ThisWorkbook.Sheets("Sheet1")
    Set wsOutput = ThisWorkbook.Sheets("Sheet2")

    '~~> Start Row in output sheet
    j = 1

    With wsMain
        '~~> Get last row in Col A & B
        lRowColA = .Range("A" & .Rows.Count).End(xlUp).Row
        lRowColB = .Range("B" & .Rows.Count).End(xlUp).Row

        '~~> Set your actual data range in Col A and B
        Set ColARng = .Range("A1:A" & lRowColA)
        Set ColBRng = .Range("B1:B" & lRowColB)

        '~~> Loop through Col A
        For i = 1 To lRowColA
            If Len(Trim(.Range("A" & i).Value)) <> 0 Then
                '~~> Check if there are duplicates of Col A value in Col B
                If Application.WorksheetFunction.CountIf(ColBRng, _
                .Range("A" & i).Value) > 0 Then
                    '~~> If found write to output sheet
                    wsOutput.Cells(j, 1).Value = .Range("A" & i).Value
                    wsOutput.Cells(j, 2).Value = .Range("A" & i).Value

                    '~~> Find the duplicate value in Col B
                    Set aCell = ColBRng.Find(What:=.Range("A" & i).Value, _
                    LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
                    SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)

                    '~~> Clear the duplicate value in Col B
                    aCell.ClearContents
                    '~~> Clear the duplicate value in Col A
                    .Range("A" & i).ClearContents

                    '~~> Set i = 1 to restart loop and increment
                    '~~> the next row for output sheet
                    i = 1: j = j + 1
                End If
            End If
        Next i

        '~~> Sort data in Col A to remove the blank spaces
        ColARng.Sort Key1:=.Range("A1"), Order1:=xlAscending, Header:=xlNo, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal

        '~~> Sort data in Col B to remove the blank spaces
        ColBRng.Sort Key1:=.Range("B1"), Order1:=xlAscending, Header:=xlNo, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
    End With
End Sub

截图

【讨论】:

  • 嗨,很抱歉回复晚了,没有工作,代码运行良好。老实说,我无法弄清楚。感谢您提供代码和 cmets - 帮助我了解有关 vba 的更多信息。感谢大家的快速回复。
  • @Siddharth,很抱歉再次打扰,之前已经测试过代码并且它有效,现在我已经输入了实际总数,代码返回不正确的结果。我一直在玩它,直到我能理解它并会尝试解释。如果我将以下总数放入第一张 5.05 5 0.55 5.19 5.5 19.55 1.9 0.19 19 5.05 19.55 0.55 5 5.5 的返回结果中 sheet1 给我一个 5,而在 sheet2 上它给我一个 5 两边。它不应该在 sheet1 上显示 5,因为它已经与 A 列中的另一个匹配。我认为这些金额彼此太接近了。感谢您的帮助。
  • 您可以在 wikisend.com 中上传包含示例值的工作簿并在此处分享链接吗?让我看看你的数据
  • 会做的,给我几分钟看看怎么做。
  • 无法从工作电脑访问任何文件托管站点。下午回家后上传。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多