如何为每隔一行着色,但跳过Column A = X
的任何行?
错误的是它在我的子标题行上的颜色。我试图让它跳过标题行,标题为X
列中不可见的A
。
是否可以跳过子标题,子标题行下面的行是白色的?有点像它重新开始。
这是我的代码,颜色为白色,然后是灰色到整个范围的末尾:
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
答案 0 :(得分:1)
您可以使用if statement扩展当前的and operator。
示例:
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的内置conditional formatting。这可能是更简单的方法。