Excel VBA 突出显示活动列中的重复项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21399691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:38:40  来源:igfitidea点击:

Excel VBA Highlight duplicates in active column

excel-vbavbaexcel

提问by Bgs14

I'm trying to create a macro that will highlight duplicates in the column where text is being entered.

我正在尝试创建一个宏,该宏将在输入文本的列中突出显示重复项。

I have 54 columns and want to highlight duplicates in each column as the text is entered. The scenario is: if "STAPLES" is entered twice in column B then the cells (B3, B22) would be highlighted. I want a macro that can do this for each column, so if "STAPLES" is entered into column E only once nothing should happen.

我有 54 列,希望在输入文本时突出显示每列中的重复项。场景是:如果在 B 列中输入两次“STAPLES”,则单元格 (B3, B22) 将突出显示。我想要一个可以为每一列执行此操作的宏,因此如果“STAPLES”仅在 E 列中输入一次,则不会发生任何事情。

Using the Conditional Formatting =COUNTIF doesn't necessarily help (due to the workflow of copying columns to new worksheets).

使用条件格式 =COUNTIF 不一定有帮助(由于将列复制到新工作表的工作流程)。

I have this macro already:

我已经有了这个宏:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)


Dim Rng As Range
Dim cel As Range

'Test for duplicates in a single column
'Duplicates will be highlighted in red


Set Rng = Range(Range("C1"), Range("C" & Rows.Count).End(xlUp))


For Each cel In Rng
    If WorksheetFunction.CountIf(Rng, cel.Value) > 1 Then

cel.Interior.ColorIndex = 3
    End If
Next cel

End Sub

It works ok but is only for one column ("C").

它工作正常,但仅适用于一列(“C”)。

How do I set the range to be the active column?

如何将范围设置为活动列?

I have tried to change Rng to

我试图将 Rng 更改为

'Set Rng = Range(ActiveCell,ActiveCell.Column.End(xlUp)) 

but this is obviously wrong.

但这显然是错误的。

Any ideas?

有任何想法吗?

回答by Dmitry Pavliv

Try this one:

试试这个:

Set Rng = Range(Cells(1, Target.Column), Cells(Rows.Count, Target.Column).End(xlUp))

and it's better to use Worksheet_Changeevent instead Worksheet_SelectionChange.

最好使用Worksheet_Changeevent 代替Worksheet_SelectionChange

Btw, there is special CF for duplicates:

顺便说一句,重复项有特殊的 CF:

enter image description here

在此处输入图片说明



UPD:If you'd like to use VBA, try following code:

UPD:如果您想使用 VBA,请尝试以下代码:

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim Rng As Range
    Dim cel As Range
    Dim col As Range
    Dim c As Range
    Dim firstAddress As String



    'Duplicates will be highlighted in red
    Target.Interior.ColorIndex = xlNone
    For Each col In Target.Columns
        Set Rng = Range(Cells(1, col.Column), Cells(Rows.Count, col.Column).End(xlUp))
        Debug.Print Rng.Address

        For Each cel In col
            If WorksheetFunction.CountIf(Rng, cel.Value) > 1 Then
                Set c = Rng.Find(What:=cel.Value, LookIn:=xlValues)
                If Not c Is Nothing Then
                    firstAddress = c.Address
                    Do
                        c.Interior.ColorIndex = 3
                        Set c = Rng.FindNext(c)
                    Loop While Not c Is Nothing And c.Address <> firstAddress
                End If
            End If
        Next
    Next col

End Sub