简体   繁体   English

突出显示与列中存在的值不同的单元格

[英]Highlight cells with different values than present in the column

I need to go through a table (X by Y) where every column (X) should have the same value (string) and would like the cell to get highlighted if is not the same value.我需要 go 通过一个表(X x Y),其中每列(X)应该具有相同的值(字符串),并且如果不是相同的值,希望单元格突出显示。 I'm stuck in building the comparison method, because I would like it to be dynamic.我一直在构建比较方法,因为我希望它是动态的。 I would like to first determine what is the value that is present the most in the column and determine that is what everything else needs to be compared against and highlight the cell that is not equal to this value.我想首先确定列中出现最多的值是什么,并确定这是需要比较的所有其他值,并突出显示不等于该值的单元格。

Example (6x5 table) -示例(6x5 表)-

A 3 4 C M R A 3 4 C M R

A 3 4 OM R A 3 4 欧姆 R

8 3 TOMF 8 3 TOMF

8 3 4 OMG 8 3 4 天哪

A 3 TOYK一个 3 玩具

In the first column, A is the most prevalent value therefore the (8s) are highlighted, second column nothing is highlighted, third both (T) are highlighted, fourth column (C), fifth column (Y) and sixth column (F),(G) and (K) are highlighted.在第一列中,A 是最普遍的值,因此 (8s) 突出显示,第二列没有突出显示,第三列 (T) 都突出显示,第四列 (C)、第五列 (Y) 和第六列 (F) , (G) 和 (K) 突出显示。

Thank you in advance.先感谢您。

I can suggest a workaround.我可以建议一个解决方法。 Not sure if this is the best way though.不确定这是否是最好的方法。

在此处输入图像描述

Step 1. Create a count if table which basically gives you the count of a value in the column.步骤 1. 创建一个 count if 表,它基本上为您提供列中值的计数。 Ex: for cell F4 use formula =COUNTIF($B$4:$B$8,B4)例如:对于单元格 F4,使用公式 =COUNTIF($B$4:$B$8,B4)

Step 2. Create a row with max values of each column.第 2 步。创建具有每列最大值的行。 Ex: Cell F11 =MAX(F4:F8)例如:单元格 F11 =MAX(F4:F8)

Step 3. For each cell, give a conditional formatting condition that if the value in the count table does not match the max value, color it.步骤 3. 对于每个单元格,给出一个条件格式条件,如果计数表中的值与最大值不匹配,则对其进行着色。 Ex: for cell B4, go to conditional formatting -> new rule -> use a formula and write this formula =F4<>F$11.例如:对于单元格 B4,go 到条件格式 -> 新规则 -> 使用公式并编写此公式 =F4<>F$11。 Then copy paste the format to all other cells.然后将格式复制粘贴到所有其他单元格。

Note: this will not work when multiple values in a column have the same max count.注意:当列中的多个值具有相同的最大计数时,这将不起作用。

Here is a VBA solution, I've commented the code so you can understand这是一个VBA解决方案,我已经注释了代码,所以你可以理解

    Sub HighlightNonFrequentInColumn()

    Dim rng As Range
    Dim col As Range
    Dim cell As Range
    Dim myVal As String
    Dim colRng As String

    Set rng = Selection '<<change range as required

    For Each col In rng.Columns
        'determine the most frequent value
        colRng = col.Address
        On Error Resume Next
        myVal = Application.Evaluate("INDEX(" & colRng & ",MODE(IF(" & colRng & "<>"""",MATCH(" & colRng & "," & colRng & ",0))))")
        If Application.countblanks(col) > Application.CountIf(col, myVal) Then myVal = "" '<<if blanks are most frequent
        'highlight all cells not equals to most frequent value
        For Each cell In col.Cells
            If Not cell = myVal Then cell.Interior.Color = vbYellow '<<change colour as desired
        Next
    Next

    End Sub

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

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