简体   繁体   English

如何使用VBA在Excel中使用其单元格值在每列中查找重复项?

[英]How to find duplicates in each column using its cell value in Excel using VBA?

I want to find the duplicates and highlight it in each column of the active worksheet in Excel 2010 using the cell value specified by us. 我想查找重复项,并使用我们指定的单元格值在Excel 2010中活动工作表的每一列中突出显示它们。 For instance, There are 5 columns namely "S.No","ID","Name","Desc", and "Amount" which is default all the time (Note: Column value is always same whereas column number may differ each time). 例如,共有5列,即“ S.No”,“ ID”,“名称”,“ Desc”和“金额”,这些列始终为默认值(注意:列值始终相同,而列号可能各不相同时间)。 So, In this case, I want to find duplicates based on column value. 因此,在这种情况下,我想根据列值查找重复项。 If column X="S.No" then, find duplicates and highlight it.. and/or if column X="ID" find duplicated and highlight it. 如果X =“ S.No”列,则查找重复项并突出显示..和/或如果X =“ ID”列,发现重复项并突出显示。

I have a code with me, but this works only for column A. Please help me in providing the updates to find all the duplicates in every column in the active worksheet as explained above. 我有一个代码,但这仅适用于A列。如上所述,请帮助我提供更新以在活动工作表的每一列中查找所有重复项。

Sub DupEntry()
Dim cel As Variant
Dim rng As Range
Dim clr As Long
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Set rng = Range("A1:A" & Range("A1048576").End(xlUp).Row)
rng.Interior.ColorIndex = xlNone
clr = 3
For Each cel In rng
If Application.WorksheetFunction.CountIf(rng, cel) > 1 Then
If WorksheetFunction.CountIf(Range("A1:A" & cel.Row), cel) = 1 Then
cel.Interior.ColorIndex = clr
clr = clr + 1
Else
cel.Interior.ColorIndex = rng.Cells(WorksheetFunction.Match(cel.Value, rng, False), 1).Interior.ColorIndex
End If
End If
Next
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub

You can achieve the desired effect by using "Conditional Formatting" feature in Excel Worksheet: 您可以通过使用Excel工作表中的“条件格式”功能来达到所需的效果:

  1. Select Column A 选择列A

  2. Click on Conditional Formatting menu button, then select "Highlight Cells Rules" and "Duplicates Values": specify the color from Drop-Down list. 单击“条件格式”菜单按钮,然后选择“突出显示单元规则”和“重复值”:从下拉列表中指定颜色。

  3. Repeat the same steps for other Columns. 对其他列重复相同的步骤。

In case you prefer to use your VBA solution which highlights duplicates with different colors, then just apply it to other Columns: see that line 如果您希望使用VBA解决方案来突出显示具有不同颜色的重复项,则可以将其应用于其他列:查看该行

Set rng = Range("A1:A" & Range("A1048576").End(xlUp).Row)

so, instead of Column "A" use Column "B", etc. I would recommend to use iteration through the specified Columns range. 因此,不要使用“ B”列,而是使用“ B”列,而不是“ A”列。我建议在指定的Columns范围内使用迭代。 With minor changes it can be implemented as shown in the following sample code snippet: 进行较小的更改即可实现,如以下示例代码片段所示:

Sub DupEntry()
Dim cel As Variant
Dim rng As Range
Dim clr As Long
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

'Sample Array of Columns
Dim Col(1 To 3) As String
Col(1) = "A"
Col(2) = "B"
Col(3) = "C"

'Iterate through Columns
For i = 1 To 3
    'Set rng = Range("A1:A" & Range("A1048576").End(xlUp).Row)
    Set rng = Range(Col(i) & "1:" & Col(i) & Range(Col(i) & "1048576").End(xlUp).Row)
    rng.Interior.ColorIndex = xlNone
    clr = 3
    For Each cel In rng
        If Application.WorksheetFunction.CountIf(rng, cel) > 1 Then
            'If Application.WorksheetFunction.CountIf(Range("A1:A" & cel.Row), cel) = 1 Then
            If Application.WorksheetFunction.CountIf(Range(Col(i) & "1:" & Col(i) & cel.Row), cel) = 1 Then
                cel.Interior.ColorIndex = clr
                clr = clr + 1
            Else
                cel.Interior.ColorIndex = rng.Cells(WorksheetFunction.Match(cel.Value, rng, False), 1).Interior.ColorIndex
            End If
        End If
    Next
Next i
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub

(original code lines are commented off). (原始代码行已被注释掉)。 Alternatively, you can use cells R1C1 notation. 或者,您可以使用单元格R1C1表示法。

Hope this will help. 希望这会有所帮助。 Best regards, 最好的祝福,

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

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