【问题标题】:Highlight duplicates across a workbook突出显示工作簿中的重复项
【发布时间】:2014-10-04 18:52:36
【问题描述】:

我正在尝试突出显示 12 张工作簿中的重复项。

我们跟踪 ID#,如果 ID#(值)在任何其他工作表上,我想突出显示单元格。

当我在“此工作簿”中使用下面的代码时,它适用于一个工作表,而不是跨多个工作表。

Private Sub Workbook_SheetChange(ByVal Sh As Object, 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

【问题讨论】:

  • 您应该能够使用For Each Worksheet In ActiveWorkbook.Worksheets 遍历每张工作表并将代码应用于每张工作表。
  • 所有工作表中的所有 ID 都在同一列中吗?
  • 是的,ID#s 都在每个工作表的“A”列中。
  • 发布了答案。看看吧
  • 您可能想刷新页面,因为我正在尝试代码并进行了一些编辑。

标签: excel vba


【解决方案1】:

此代码的作用是遍历工作表中被激活的 Col A 的值,然后搜索所有剩余工作表的 Col A,如果找到 ID,则将单元格背景着色为红色。

久经考验

我已经对代码进行了注释,因此您理解它应该没有问题。如果你仍然这样做,那么只需回帖:)

试试这个

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    Dim lRow As Long, wsLRow As Long, i As Long
    Dim aCell As Range
    Dim ws As Worksheet
    Dim strSearch As String

    With Sh
        '~~> Get last row in Col A of the sheet
        '~~> which got activated
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row

        '~~> Remove existing Color from the column
        '~~> This is to cater for any deletions in the
        '~~> other sheets so that cells can be re-colored
        .Columns(1).Interior.ColorIndex = xlNone

        '~~> Loop through the cells of the sheet which
        '~~> got activated
        For i = 1 To lRow
            '~~> Store the ID in a variable
            strSearch = .Range("A" & i).Value

            '~~> loop through the worksheets in the workbook
            For Each ws In ThisWorkbook.Worksheets
                '~~> This is to ensure that it doesn't
                '~~> search itself
                If ws.Name <> Sh.Name Then
                    '~~> Get last row in Col A of the sheet
                    wsLRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row

                    '~~> Use .Find to quick check for the duplicate
                    Set aCell = ws.Range("A1:A" & wsLRow).Find(What:=strSearch, _
                                                               LookIn:=xlValues, _
                                                               LookAt:=xlWhole, _
                                                               SearchOrder:=xlByRows, _
                                                               SearchDirection:=xlNext, _
                                                               MatchCase:=False, _
                                                               SearchFormat:=False)

                    '~~> If found then color the cell red and exit the loop
                    '~~> No point searching rest of the sheets
                    If Not aCell Is Nothing Then
                        Sh.Range("A" & i).Interior.ColorIndex = 3
                        Exit For
                    End If
                End If
            Next ws
        Next i
    End With
End Sub

【讨论】:

  • 嗨,悉达多,感谢您的帮助。这几乎可以按预期工作,有几件事我想更改:#1:我现在更改为 sheet_selectionchange(不知道为什么我从一开始就没有使用它)和#2:我也需要它检查同一张纸上的重复项(似乎无法按原样使用代码)..
  • 另外,如果您有类似的数字组合(即 123456 和 654321),它会将它们都突出显示为重复项...
【解决方案2】:

这是一个简化的示例,它可以为您提供一些想法并为您指明正确的方向。 如果您有任何问题,请告诉我。

Sub collected_ids_example()
    ' enable microsoft scripting runtime --> tools - references
    ' For convenience I put all code in 2 subs/functions
    ' This code assumes you want every cell with a duplicate id highlighted.
    ' Although it is easy enough to modify that if you want.

    Dim sh As Worksheet
    Dim id_to_addresses As New Dictionary
    Dim id_ As Range

    ' For every worksheet collect all ids and their associated adressses
    ' for the specified range.
    For Each sh In ThisWorkbook.Sheets
        For Each id_ In sh.Range("A4:A100")
            If Not IsEmpty(id_) Then
                If Not id_to_addresses.Exists(id_.Value) Then
                    Set id_to_addresses(id_.Value) = New Collection
                End If
                id_to_addresses(id_.Value).Add get_full_address(id_)
            End If
        Next id_
    Next sh

    ' Color each cell with a duplicate id
    Dim collected_id As Variant
    Dim adresses As Collection
    Dim c As Range
    For Each collected_id In id_to_addresses
        Dim duplicate_address As Variant
        Set adresses = id_to_addresses(collected_id)

        'You have a duplicate if an id is associated with more than 1 addrress
        If adresses.Count >= 2 Then
            For Each duplicate_address In adresses
                Set c = Range(duplicate_address)
                c.Interior.ColorIndex = 3
            Next duplicate_address
        End If
    Next collected_id
End Sub

Private Function get_full_address(c As Range) As String
    get_full_address = "'" & c.Parent.Name & "'!" & c.Address(External:=False)
End Function

【讨论】:

  • 嗨,弗兰克。我对 VBA 很陌生,您能否调整此代码以突出显示每张纸上的单元格范围 A4:A100?
  • 我按照您的要求更改了代码。如果您还有任何问题,请告诉我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-24
  • 2016-09-07
  • 2021-02-09
  • 2016-10-29
  • 1970-01-01
  • 1970-01-01
  • 2016-03-13
相关资源
最近更新 更多