【问题标题】:Alternating coloring groups of rows in Excel在 Excel 中交替着色行组
【发布时间】:2010-09-06 19:43:30
【问题描述】:

我有一个这样的 Excel 电子表格

编号 | id 的数据 | id 的更多数据 编号 | id 的数据 编号 | id 的数据 | id 的更多数据 | id 的更多数据 编号 | id 的数据 | id 的更多数据 编号 | id 的数据 编号 | id 的数据 | id 的更多数据

现在我想通过交替行的背景颜色来对一个 id 的数据进行分组

var 颜色 = 白色 对于每一行 如果第一个单元格不为空且颜色为白色 将颜色设置为绿色 如果第一个单元格不为空且颜色为绿色 将颜色设置为白色 将行的背景设置为颜色

任何人都可以帮助我使用宏或一些 VBA 代码

谢谢

【问题讨论】:

    标签: excel vba colors


    【解决方案1】:

    我使用这个公式来获取条件格式的输入:

    =IF(B2=B1,E1,1-E1))    [content of cell E2]
    

    其中B列包含需要分组的项目,E是辅助列。每次上部单元格(本例中为 B1)与当前单元格(B2)相同时,返回 E 列的上部行内容。否则,它将返回 1 减去该内容(即输出将为 0 或 1,具体取决于上部单元格的值)。

    【讨论】:

    • 就我而言,我不能在公式中使用分号 (;),Excel 只能接受逗号 (,)。我正在使用 MS Excel v 14.0.7106.5003 32 位。
    • 是否可以避免使用额外的列?
    • @salman-a,如果没有那个额外的 col,我想不出一个解决方案,但你可以隐藏它。
    • 很好的答案。根据您的 Excel 版本,使用以下规则描述公式可能会更好:=$P2=1
    【解决方案2】:

    我认为这可以满足您的需求。当 A 列中的单元格更改值时翻转颜色。一直运行到 B 列中没有值为止。

    Public Sub HighLightRows()
        Dim i As Integer
        i = 1
        Dim c As Integer
        c = 3       'red
    
        Do While (Cells(i, 2) <> "")
            If (Cells(i, 1) <> "") Then    'check for new ID
                If c = 3 Then
                    c = 4   'green
                Else
                    c = 3   'red
                End If
            End If
    
            Rows(Trim(Str(i)) + ":" + Trim(Str(i))).Interior.ColorIndex = c
            i = i + 1
        Loop
    End Sub
    

    【讨论】:

    • 我可能误解了,但是从我看到它的地方和我的测试来看,只要 A 和 B 列不为空,这就会逐行切换颜色......所以不回答这个问题.但我可能错过了一些东西......
    【解决方案3】:

    根据 Jason Z 的回答,我的测试似乎是错误的(至少在 Excel 2010 上),这里有一些代码恰好对我有用:

    Public Sub HighLightRows()
        Dim i As Integer
        i = 2 'start at 2, cause there's nothing to compare the first row with
        Dim c As Integer
        c = 2       'Color 1. Check http://dmcritchie.mvps.org/excel/colors.htm for color indexes
    
        Do While (Cells(i, 1) <> "")
            If (Cells(i, 1) <> Cells(i - 1, 1)) Then 'check for different value in cell A (index=1)
                If c = 2 Then
                    c = 34   'color 2
                Else
                    c = 2   'color 1
                End If
            End If
    
            Rows(Trim(Str(i)) + ":" + Trim(Str(i))).Interior.ColorIndex = c
            i = i + 1
        Loop
    End Sub
    

    【讨论】:

    • 我最初在 08 年回答了这个问题,所以如果 Office 2010 破坏了 Office 2007 的某些内容,我一点也不感到惊讶。
    • 这个对我有用。谢谢你..我有一个问题。当我运行这个 VBA 代码时,它的颜色完美,但它也删除了列线/边框。是否可以不删除这些行?并为行着色?
    • @AmanDevrath 我建议您针对该请求编写自己的具体问题。我最好的猜测是这很有可能而且很容易......
    • 好的。谢谢。
    【解决方案4】:

    你必须使用代码吗? 如果表格是静态的,那为什么不使用自动格式化功能呢?

    如果您“合并”相同数据的单元格,它也可能会有所帮助。所以也许如果将“数据,更多数据,甚至更多数据”的单元格合并到一个单元格中,您可以更轻松地处理经典的“每一行都是一行”的情况。

    【讨论】:

    • 这是我需要的,但不知道该怎么做 :) 基于提供的解释
    【解决方案5】:

    我正在挖掘这个并试图修改它以供我使用。我在 a 列中有订单号,有些订单需要多行。只想按订单号交替使用白色和灰色。我在这里的每一行都是交替的。

    ChangeBackgroundColor() ' ChangeBackgroundColor Macro ' ' Keyboard Shortcut: Ctrl+Shift+B Dim a As Integer a = 1 Dim c As Integer c = 15 'gray Do While (Cells(a, 2) <> "") If (Cells(a, 1) <> "") Then 'check for new ID If c = 15 Then c = 2 'white Else c = 15 'gray End If End If Rows(Trim(Str(a)) + ":" + Trim(Str(a))).Interior.ColorIndex = c a = a + 1 Loop

    结束子

    【讨论】:

      【解决方案6】:

      如果您选择“格式”菜单项下的“条件格式”菜单选项,您将看到一个对话框,让您可以构建一些应用于该单元格的逻辑。

      您的逻辑可能与您上面的代码不同,它可能看起来更像:

      单元格值为 |等于 | |和 |白色....然后选择颜色。

      您可以选择添加按钮并根据需要使条件变大。

      【讨论】:

        【解决方案7】:

        我已经根据可配置的列,使用 RGB 值重新设计了 Bartdude 的答案,即浅灰色/白色。当值发生变化时,布尔变量被翻转,这用于通过 True 和 False 的整数值来索引颜色数组。在 2010 年为我工作。用工作表号致电分公司。

        Public Sub HighLightRows(intSheet As Integer)
            Dim intRow As Integer: intRow = 2 ' start at 2, cause there's nothing to compare the first row with
            Dim intCol As Integer: intCol = 1 ' define the column with changing values
            Dim Colr1 As Boolean: Colr1 = True ' Will flip True/False; adding 2 gives 1 or 2
            Dim lngColors(2 + True To 2 + False) As Long   ' Indexes : 1 and 2
                  ' True = -1, array index 1.    False = 0, array index 2.
            lngColors(2 + False) = RGB(235, 235, 235) ' lngColors(2) = light grey
            lngColors(2 + True) = RGB(255, 255, 255) '  lngColors(1) = white
        
            Do While (Sheets(intSheet).Cells(intRow, 1) <> "")
                'check for different value in intCol, flip the boolean if it's different
                If (Sheets(intSheet).Cells(intRow, intCol) <> Sheets(intSheet).Cells(intRow - 1, intCol)) Then Colr1 = Not Colr1
                Sheets(intSheet).Rows(intRow).Interior.Color = lngColors(2 + Colr1) ' one colour or the other
                ' Optional : retain borders (these no longer show through when interior colour is changed) by specifically setting them
                With Sheets(intSheet).Rows(intRow).Borders
                    .LineStyle = xlContinuous
                    .Weight = xlThin
                    .Color = RGB(220, 220, 220)
                End With
                intRow = intRow + 1
            Loop
        End Sub
        

        可选奖励:对于 SQL 数据,使用与 SSMS 中相同的黄色为任何 NULL 值着色

        Public Sub HighLightNULLs(intSheet As Integer)
            Dim intRow As Integer: intRow = 2 ' start at 2 to avoid the headings
            Dim intCol As Integer
            Dim lngColor As Long: lngColor = RGB(255, 255, 225) ' pale yellow
        
            For intRow = intRow To Sheets(intSheet).UsedRange.Rows.Count
                For intCol = 1 To Sheets(intSheet).UsedRange.Columns.Count
                    If Sheets(intSheet).Cells(intRow, intCol) = "NULL" Then Sheets(intSheet).Cells(intRow, intCol).Interior.Color = lngColor
                Next intCol
            Next intRow
        End Sub
        

        【讨论】:

          【解决方案8】:

          我在 Excel 中使用此规则来格式化交替行:

          1. 突出显示您希望对其应用交替样式的行。
          2. 按“条件格式” -> 新规则
          3. 选择“使用公式确定要格式化的单元格”(最后一个条目)
          4. 在格式值中输入规则:=MOD(ROW(),2)=0
          5. 按“格式”,为交替行设置所需的格式,例如。填充 -> 颜色。
          6. 按 OK,按 OK。

          如果您希望设置交替列的格式,请使用 =MOD(COLUMN(),2)=0

          瞧!

          【讨论】:

          • 您好,您回答了备用行颜色,问题想要为数据组着色,而不是现在..
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-06-15
          • 2011-01-19
          • 1970-01-01
          • 2010-10-02
          • 1970-01-01
          • 1970-01-01
          • 2018-06-28
          相关资源
          最近更新 更多