简体   繁体   English

如何在单元格值更改时交替表格中的行颜色?

[英]How to alternate row color in a table when cell value changes?

I have a spreadsheet that brings in a table of data via Power Query.我有一个通过 Power Query 引入数据表的电子表格。
Every time there is a refresh, the table length may change.每次刷新,表的长度都可能改变。

I need columns C and D to alternate highlight colors when the value is not the same as the previous row.当值与上一行不同时,我需要列 C 和 D 交替突出显示 colors。

How the table should look each time it is refreshed through Power Query and the VBA code runs.每次通过 Power Query 刷新表和运行 VBA 代码时表的外观。
这是我的工作表的屏幕截图

The VBA to apply the format condition for the alternating coloring would be:应用交替着色格式条件的 VBA 将是:

Public Sub alternatingColorSizeAndKind(rg As Range)

Dim fc As FormatCondition
With rg.FormatConditions
    .Delete
    Set fc = .Add(xlExpression, , "=($C1=$C2)+($C2=$C3)")
    fc.Interior.Color = 14348258
    
    Set fc = .Add(xlExpression, , "=($C1<>$C2)")
    fc.Interior.Color = 13431551
End With
    
End Sub

You have to pass the range of your table to this sub.您必须将表格的范围传递给此子。

If you have a listobject/table then you call it like this:如果你有一个列表对象/表,那么你可以这样称呼它:

Public Sub update()


Dim lo As ListObject
'>>> adjust the names to your needs
Set lo = ThisWorkbook.Worksheets("Sheet1").ListObjects("Pull_Data")

alternatingColorSizeAndKind lo.DataBodyRange

End Sub

(1) Attempt with conditional formatting: (1)尝试条件格式:
(Note: This will work correctly only if a value cannot appear later down that list). (注意:只有当值不能出现在该列表的后面时,这才会正常工作)。

Create a rule (or two rules, to be precise) based on a formula.基于公式创建一个规则(或两个规则,准确地说)。 According to your screenshot, I assume that your data starts at row 3 and you want to look at column C.根据您的屏幕截图,我假设您的数据从第 3 行开始并且您想查看第 C 列。
There is a rather easy formula that you can use to count the number of unique values of a list.您可以使用一个相当简单的公式来计算列表中唯一值的数量。 The base formula was "borrowed" from ExcelJet and is =SUMPRODUCT(1/COUNTIF(data,data)) .基本公式是从ExcelJet “借用”的,是=SUMPRODUCT(1/COUNTIF(data,data)) The trick now is that you look to the range from the beginning of the list to the actual row by using the range C$3:C3 .现在的诀窍是使用范围C$3:C3查看从列表开头到实际行的范围。 If you copy the formula down, the start row remains while the end row is changed.如果向下复制公式,起始行将保留,而结束行将更改。 Now simple put a IsOdd resp IsEven around the formula.现在简单地在公式周围放置一个IsOdd resp IsEven

So the formula is =ISODD(SUMPRODUCT(1/COUNTIF(C$3:C3,C$3:C3))) for the "green" rows and =ISEVEN(SUMPRODUCT(1/COUNTIF(C$3:C3,C$3:C3))) for the yellow.因此,“绿色”行的公式为=ISODD(SUMPRODUCT(1/COUNTIF(C$3:C3,C$3:C3)))=ISEVEN(SUMPRODUCT(1/COUNTIF(C$3:C3,C$3:C3)))为黄色。 Apply the rule to the range =$C$3:$C$1048576将规则应用到范围=$C$3:$C$1048576

However, I don't know if this conditional formatting will "survive" an update of the query.但是,我不知道这种条件格式是否会在查询更新后“存活下来”。

在此处输入图像描述

(2) Formatting with VBA is simple. (2)用 VBA 格式化很简单。 The following code formats one column of an table (a table in VBA is the type ListObject . Pass the listobject and the column number as parameter:以下代码格式化表格的一列(VBA 中的表格是ListObject类型。将 listobject 和列号作为参数传递:

Sub ColorRows(table As ListObject, columnNumber As Long)
    Dim cell As Range, isOdd As Boolean
    For Each cell In table.DataBodyRange.Columns(columnNumber).Cells
        With cell
            If .Offset(-1, 0) <> .Value Then isOdd = Not isOdd
            ' On a standard Office color scheme, 10 is kind of green and 8 is a dirty yellow
            .Interior.ThemeColor = IIf(isOdd, 10, 8)
            .Interior.TintAndShade = 0.8
        End With
    Next
End Sub

This is how the call could look like (adapt the sheet and the listObject to your needs):这就是调用的样子(根据您的需要调整工作表和 listObject):

Sub test()
    ColorRows ThisWorkbook.Sheets(1).ListObjects(1), 3
End Sub

Now calling this code automatically is a different story and unfortunately rather complicated - if you want/need, tryhttps://stackoverflow.com/search?q=vba+QueryTable+After+Refresh for some insights.现在自动调用此代码是另一回事,不幸的是相当复杂 - 如果您想要/需要,请尝试https://stackoverflow.com/search?q=vba+QueryTable+After+Refresh以获得一些见解。 An easy alternative is to trigger the formatting manually, eg by placing a button (or a shape) on your sheet that calls the code.一个简单的替代方法是手动触发格式化,例如通过在您的工作表上放置一个调用代码的按钮(或形状)。

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

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