我怎样才能为其他行着色,但跳过标记为X的行?

我怎样才能颜色每隔一行,但跳过Column A = X任何行?

什么是错的是它的颜色在我的子标题行。 我试图让它跳过列A不可见X标记的标题行。

可以跳过子标题,子标题行下面的行是白色的吗? 有点像重新开始。

这是我的代码,颜色行白色,然后灰色到整个范围的最后:

 Sub Format() Application.ScreenUpdating = False Dim sht2 As Worksheet Set sht2 = ThisWorkbook.Worksheets("Email Form") sht2.Activate sht2.Unprotect Dim LastRow As Long, LastCol As Long Dim rng As Range Dim WholeRng As Range With sht2 Set rng = Cells LastRow = rng.Find(What:="*", After:=rng.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row LastCol = rng.Find(What:="*", After:=rng.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False).Column Set WholeRng = Range(Cells(4, "B"), Cells(LastRow, LastCol)) WholeRng.Select With WholeRng With .Interior .PatternColorIndex = xlAutomatic .Color = RGB(255, 255, 255) .TintAndShade = 0 Range(Cells(4, "B"), Cells(LastRow, LastCol)).Borders(xlInsideHorizontal).LineStyle = xlContinuous Range(Cells(4, "B"), Cells(LastRow, LastCol)).Borders(xlInsideVertical).LineStyle = xlContinuous Range(Cells(4, "B"), Cells(LastRow, LastCol)).Borders(xlEdgeBottom).LineStyle = xlContinuous Range(Cells(4, "B"), Cells(LastRow, LastCol)).Borders(xlEdgeRight).LineStyle = xlContinuous End With End With Dim b As Boolean For Each rng In WholeRng.Rows If Not rng.Hidden Then If b Then rng.Interior.Color = Black b = Not b End If Next End With Set rng = Nothing Set WholeRng = Nothing Set sht2 = Nothing Application.ScreenUpdating = True End Sub 

您可以使用和运算符扩展当前的if语句 。

例:

 Dim b As Boolean For Each rng In WholeRng.Rows If Not rng.Hidden Then ' UPDATED LINE BELOW. If b And sht2.Cells(rng.Row, 1) <> "x" Then rng.Interior.Color = Black b = Not b End If Next 

该代码从rng对象中提取当前行号。 它使用它来查看列a的内容。

另一种方法是使用Excel的内置条件格式 。 这可能是更简单的方法。