Vba 如何为其他行着色,但跳过标记为X的行?
如何为其他行着色,但跳过列A=X的任何行 错的是它在我的副标题行上标了颜色。我试图让它跳过标题行,标题行在A列中由一个不可见的X标记 是否可以跳过子标题,并且子标题行下方的行为白色?有点像重新开始 这是我的代码,在整个范围内将行涂成白色,然后涂成灰色:Vba 如何为其他行着色,但跳过标记为X的行?,vba,excel,Vba,Excel,如何为其他行着色,但跳过列A=X的任何行 错的是它在我的副标题行上标了颜色。我试图让它跳过标题行,标题行在A列中由一个不可见的X标记 是否可以跳过子标题,并且子标题行下方的行为白色?有点像重新开始 这是我的代码,在整个范围内将行涂成白色,然后涂成灰色: Sub Format() Application.ScreenUpdating = False Dim sht2 As Worksheet Set sht2 = ThisWorkbook.Worksheets("Email Form") sh
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
您可以使用 例如:
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内置的。这可能是更简单的方法。只需几个注释-如果工作表为空,则LastRow、LastCol变量可能不包含任何内容,并且整个工作表将因此抛出错误。不需要整个选择线-您可以在不选择的情况下为内饰上色。您可能还希望调整整个页面的大小以添加边框,而不是使用ActiveSheet上的LastRow、LastCol。您尚未使用范围引用限定工作表名称。是否需要VBA?试想一下:如果您使用基于公式的条件格式,并将其用作公式条件:=offseture,1-Column,0==x=>这将查找每个单元格,向右移动1-Column或向左移动-1-Column,最后进入a列,然后检查值是否等于x。只有一个问题:有人知道如何填写抵销公式的第一个参数吗?如何说:从此单元格开始偏移?