【问题标题】:EXCEL: highlighting reoccuring data in the same columnEXCEL:突出显示同一列中重复出现的数据
【发布时间】:2017-12-27 09:21:33
【问题描述】:

我在 Excel 中有一列 (D) 数据,已使用以下命令进行排序: =TEXT(B2,"###")。 这是为了显示一个带有附加“REP 1”的数据列表(数字)。

并非所有数据都有“REP 1”,所以我想突出显示所有包含BOTH数字和“REP 1”的字段。 我可以突出显示所有“REP 1”字段,并查看它之前是否有重复,但这只是一个示例表。我有超过 8,000 多个字段要处理,而且太耗时了。

示例请参见以下链接:

Required Formatting

我希望这一切都有意义。 谢谢,

提姆。

【问题讨论】:

    标签: excel formatting conditional cell formula


    【解决方案1】:

    不确定它是否可能与条件格式有关,但此 VBA 代码应该可以工作。您的数据不必按任何特定顺序排序,并假设您正在格式化的数据在 D 列中。我已经在 100 行上进行了测试,它工作正常,所以对于大型数据集应该没问题。我试图通过代码中的 cmets 来解释代码在做什么。

                Sub formatCells()
    
    
                Dim x As Variant
                Dim y As Variant
                Dim searchval As String
                Dim a As Variant
                Dim lastrow As Long
                Dim rng As Range
    
                Application.ScreenUpdating = False ' turn off screen updates
    
                lastrow = Cells(Rows.Count, 4).End(xlUp).Row 'find the last blank cell
                x = 2 'set rownumber
                y = 4 'set columnnumber
    
    
                While Cells(x, y) <> "" ' create loop
                    If InStr(Cells(x, y), "REP1") Then 'search for string in cell
                        Cells(x, y).Interior.Color = RGB(255, 0, 0) 'if string exists fill cell
    
                    End If
    
                x = x + 1 ' loop
    
                Wend ' end loop
    
                x = 2 ' reset row number
                y = 4 ' reset column number
    
                While Cells(x, y) <> "" ' create loop 2
                    If Cells(x, y).Interior.Color = RGB(255, 0, 0) And InStr(Cells(x, y), "REP1") Then 'if cells is red and contains Rep1
    
                        a = Cells(x, y).Value ' set a to equal the cell that is red and and contains REP1
                            searchval = Left(a, Len(a) - 5) 'remove space and REP1 and set value ready for search
    
                        If searchval <> "" Then 'if theres a search value available run steps below
    
                            With Range("D1:D" & lastrow) 'set range to be column A
                                Set rng = .Find(What:=searchval, _
                                            After:=.Cells(1), _
                                            LookIn:=xlValues, _
                                            LookAt:=xlWhole, _
                                            SearchOrder:=xlByRows, _
                                            SearchDirection:=xlPrevious, _
                                            MatchCase:=False)
                                If Not rng Is Nothing Then 'If search value is found
                                    Application.Goto rng, True ' go to cell
                                    ActiveCell.Interior.Color = RGB(255, 0, 0) 'set cell to red
                                 End If
                            End With
    
                        End If
                    End If
    
                 x = x + 1 'loop 2
    
                Wend ' end loop 2
    
                End Sub
    

    EDIT - 查看 B 列而不是 D

                Sub formatCells()
    
    
            Dim x As Variant
            Dim y As Variant
            Dim searchval As String
            Dim a As Variant
            Dim lastrow As Long
            Dim rng As Range
    
            Application.ScreenUpdating = False ' turn off screen updates
    
            lastrow = Cells(Rows.Count, 2).End(xlUp).Row 'find the last blank cell
            x = 2 'set rownumber
            y = 2 'set columnnumber
    
    
            While Cells(x, y) <> "" ' create loop
                If InStr(Cells(x, y), "REP1") Then 'search for string in cell
                    Cells(x, y).Interior.Color = RGB(255, 0, 0) 'if string exists fill cell
    
                End If
    
            x = x + 1 ' loop
    
            Wend ' end loop
    
            x = 2 ' reset row number
            y = 2 ' reset column number
    
            While Cells(x, y) <> "" ' create loop 2
                If Cells(x, y).Interior.Color = RGB(255, 0, 0) And InStr(Cells(x, y), "REP1") Then 'if cells is red and contains Rep1
    
                    a = Cells(x, y).Value ' set a to equal the cell that is red and and contains REP1
                        searchval = Left(a, Len(a) - 5) 'remove space and REP1 and set value ready for search
    
                    If searchval <> "" Then 'if theres a search value available run steps below
    
                        With Range("B1:B" & lastrow) 'set range to be column A
                            Set rng = .Find(What:=searchval, _
                                        After:=.Cells(1), _
                                        LookIn:=xlValues, _
                                        LookAt:=xlWhole, _
                                        SearchOrder:=xlByRows, _
                                        SearchDirection:=xlPrevious, _
                                        MatchCase:=False)
                            If Not rng Is Nothing Then 'If search value is found
                                Application.Goto rng, True ' go to cell
                                ActiveCell.Interior.Color = RGB(255, 0, 0) 'set cell to red
                             End If
                        End With
    
                    End If
                End If
    
             x = x + 1 'loop 2
    
            Wend ' end loop 2
    
            End Sub
    

    【讨论】:

    • 嗨,艾米,感谢您的回复。通读一遍,这一切都说得通,但是在插入模块并运行代码之后,什么都没有发生。所有需要格式化的信息都在 B 列中,我已经对其进行了修改,但仍然没有。也许我今天只是有点慢,但这没有意义。 :( 感谢您的帮助
    • 我做了更改以查看 B 列。让我知道这是否有效?
    • 如果这不起作用,您是否在 B 列中有任何空白单元格?
    • 谢谢,但我遇到了同样的问题。虽然,现在它 正在 突出显示 B 列中的文本(其中大多数带有 和另一个带有 的单元格,但是,一些单元格被突出显示,其中仅包含 不是两者都有。这有意义吗?谢谢:)
    • 所有单元格是否包含相同数量的字符?都遵循相同的模式?您是否可以附上未突出显示的数据示例? :)
    猜你喜欢
    • 2015-08-01
    • 1970-01-01
    • 2014-02-19
    • 2019-11-06
    • 2017-01-31
    • 2012-04-18
    • 1970-01-01
    • 2022-07-22
    • 1970-01-01
    相关资源
    最近更新 更多