【问题标题】:Excel VBA mark duplicate on colA (work on all worksheet include activesheet)在 colA 上的 Excel VBA 标记重复(所有工作表上的工作包括活动表)
【发布时间】:2017-07-01 05:16:58
【问题描述】:

我想在工作簿中的所有工作表上标记重复。只有在其他工作表上存在重复项时,代码标记下方才会重复。 如果它们存在于 Activesheet 上,我也想标记它们。 (如果重复只存在于 Activesheet 中,如果可以用不同的颜色标记会更好)

这是类似案例的解决方案链接,我需要解决的问题。 [a link](https://stackoverflow.com/a/25252503/5493335) "循环遍历工作表中 Col A 的值,该值被激活,然后搜索所有剩余工作表的 Col A,如果找到 ID,则将单元格背景着色为红色。由悉达特·劳特"

我只对该代码添加了一项更改以消除空行上的颜色。 但是只有当重复是另一个工作表时,这些代码才被标记(红色)。 如果我在活动工作表上发现重复,我想知道是否可以使用不同的颜色。

我会尝试做自己并用 else 改变条件,但它不起作用。谁能帮我解决这个问题。

提前致谢。

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
                if strSearch <> "" then 'eliminated color empty cell

                '~~> 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
                   End if 
            Next i
        End With
    End Sub

【问题讨论】:

  • 删除If ws.Name &lt;&gt; Sh.Name Then 行,如果下面与它一致则结束。

标签: excel vba duplicates find


【解决方案1】:

我将对您的代码进行以下重构:

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    Dim IDsRng As Range, IDCell As Range
    Dim ws As Worksheet
    Dim strSearch As String
    Dim foundInOtherSheet As Boolean, foundInActiveSheet As Boolean

    With Sh
        Set IDsRng = .Range("A1", .Cells(.Rows.count, 1).End(xlUp)) '<--| set the IDs range as all column A not empty cells with some "text" content
        '~~> 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
    End With


    For Each IDCell In IDsRng '<--| Loop through ID cells (i.e. column A "text" cells of the activated sheet)
        '~~> Store the ID in a variable
        strSearch = IDCell.Value

        foundInActiveSheet = WorksheetFunction.CountIf(IDsRng, strSearch) > 1 '<--| count possible dupes in active sheet
        foundInOtherSheet = False '<--| initialize it at every new ID

        '~~> 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
                With ws
                    foundInOtherSheet = WorksheetFunction.CountIf(.Range("A1", .Cells(.Rows.count, 1).End(xlUp)), strSearch) > 1
                    If foundInOtherSheet Then Exit For '~~> If found then color then no point searching rest of the sheets
                End With
            End If
        Next

        Select Case True '<--| now act accordingly to where duplicates have been found
            Case foundInOtherSheet And Not foundInActiveSheet '<--| if duplicates found in "other" sheets only
                IDCell.Interior.ColorIndex = 3 '<--| red
            Case foundInOtherSheet And foundInActiveSheet '<--| if duplicates found in "other" sheets and in "active" one too
                IDCell.Interior.ColorIndex = 6 '<--| yellow
            Case Not foundInOtherSheet And foundInActiveSheet '<--| if duplicates found in "active" sheets only
                IDCell.Interior.ColorIndex = 14 '<--| green
        End Select

    Next
End Sub

【讨论】:

  • 你好。感谢您回复 User3598756。它在您的计算机上工作,因为我有错误?作为重要信息,我需要比较数字而不是文本。但是对于测试,我尝试使用文本进行测试,但我仍然使用此行进行 eroor 1004 调试 [code]In line foundInActiveSheet = WorksheetFunction.CountIf(IDsRng, strSearch) > 1[/code]
  • 不客气。如果我的回答解决了您的问题,请通过单击答案旁边的复选标记将其从灰色切换为已填写,将其标记为已接受。谢谢!
  • user3598756 - 我写道,您的代码不起作用。 * 我需要检查数字是否重复。 ** 我将代码放入工作簿中。
  • 编辑删除所有Specialcells() 以便 ID 可以是您需要的任何值
【解决方案2】:

If ws.Name &lt;&gt; Sh.NameThen线和end if下面的线去掉。

【讨论】:

  • 它不工作内森。我在尝试改变许多条件之前写了这个。删除这些行后,代码在红色everyValue 上标记,不仅重复...... Nathan 不像看起来那么容易。感谢您对这些问题的关注。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-09
  • 1970-01-01
  • 2014-04-18
相关资源
最近更新 更多