Excel 后台格式化电子表格中的行对

Excel 后台格式化电子表格中的行对,excel,background-color,vba,Excel,Background Color,Vba,我需要一种方法来格式化工作表中具有相同背景颜色的交替行对。环顾四周时,我发现以下链接: 我的问题与此类似,只是我不希望每隔一行自动填充一个背景色,而是需要对相邻的行进行着色。例如,从第4行开始,列A:T将被填充,第5行列A:T将具有相同的背景色,第6行和第7行A:T将没有背景色,第8行和第9行将与第4行和第5行共享背景色,重复到电子表格结束 为此,我尝试使用条件格式,但1)我无法从起点开始为每对行交替设置背景,2)它覆盖了少数具有不同背景颜色的特殊情况3)条件格式不允许我手动设置条件格式的任何

我需要一种方法来格式化工作表中具有相同背景颜色的交替行对。环顾四周时,我发现以下链接:

我的问题与此类似,只是我不希望每隔一行自动填充一个背景色,而是需要对相邻的行进行着色。例如,从第4行开始,列
A:T
将被填充,第5行列
A:T
将具有相同的背景色,第6行和第7行
A:T
将没有背景色,第8行和第9行将与第4行和第5行共享背景色,重复到电子表格结束

为此,我尝试使用条件格式,但1)我无法从起点开始为每对行交替设置背景,2)它覆盖了少数具有不同背景颜色的特殊情况3)条件格式不允许我手动设置条件格式的任何行的格式格式化函数已格式化

非常感谢评论者的建议(这使我走上了正确的道路),但由于条件格式的限制,我拼凑了以下宏,允许根据我的需要对背景进行格式设置,而不排除针对特殊情况进行更正的能力。对代码进行了大量注释,以帮助其他新手理解代码的含义,以及修改哪些内容以更改宏的行为

Sub AlternateRowColors()

''PURPOSE:  To format the background color of alternating pairs of rows in a designated range of cells with values.
''          A correction to account for a possible empty row at the end of the range not having a value failing to follow the
''          desired pattern is included.
''          Example:            Column A
''                        Row 1:  Green
''                        Row 2:  Green
''                        Row 3:  No Background Color
''                        Row 3:  No Background Color
''          Repeating this pattern until the end of the used cells of a worksheet.

Dim lastUsedRow As Long                     ''Variable to hold the last row number.

lastUsedRow = Range("A200").End(xlUp).Row   ''This checks backwards from cell A200 to A1 for the last row with an entry in Column A
                                            ''and saves the row number in variable lastUsedRow. Modify this as needed for each worksheet.

If lastUsedRow Mod 2 = 0 Then               ''This block of code corrects for the second row of an entry not being highlighted at the
    lastUsedRow = lastUsedRow + 1               ''end of the worksheet if the first cell in the following row is blank.
        Else
End If

For Each cell In Range("A4:T" & lastUsedRow)    ''Sets the range of rows and columns that the background color formatting is to affect.
    If cell.Row Mod 4 = 0 Then                  ''Highlight row if the row number has a divided remainder of zero
        cell.Interior.ColorIndex = 35           ''Sets background color by using .ColorIndex instead of RGB.  Change to suit your need.
            Else
        If cell.Row Mod 4 = 1 Then              ''Highlight row if the row number has a divided remainder of 1
            cell.Interior.ColorIndex = 35       ''Sets background color by using .ColorIndex instead of RGB.  Change to suit your need.
        End If
    End If
Next cell

End Sub
''ADDITIONAL NOTES:
''NONE

在条件格式>中尝试以下公式,使用公式确定要格式化的单元格:

  • =AND(ROW()>3,MOD(ROW(),4)=1)

  • =MOD(ROW(),4)=0

两者都适用于$A:$T

将特殊格式单元格的规则放在这些一般规则之后


希望这有帮助。

在条件格式中使用公式

=Mod(Row(),4) < 2
=Mod(Row(),4)<2

在您希望规则应用于

(+1)的单元格中,只需进行一个小的修改,因为OP要求格式以第4行
=和(MOD(row(),4)<2,row()>3)
开始,我的想法是,OP在决定将格式应用于哪些单元格时,将涵盖关于第4行开始的格式的规定。毕竟,如果规则没有明确反映列在A和t之间的规定,为什么它应该明确反映从第4行开始的规定?另一方面,选择由整列组成的范围比选择从第4行开始的范围更容易(除非您熟练地使用ctrl+shift+down arrow)因此,您的修改可能会使其更加方便。如果电子表格是连续的成对行,则修改将非常有用,但其中一个缺点是电子表格中的行有时用作标题。使用条件格式将需要我遍历、突出显示一个范围并重新应用格式设置,以使=Mod(Row(),4)