【发布时间】:2017-06-13 19:14:18
【问题描述】:
我正在处理一些自定义格式、带状行和列以及它们相交的位置,突出显示为较暗的阴影。
两个程序一起工作。第一个(RangeBanding)按预期工作,并对偶数行和列进行分组。
当我运行第二个(IntersectColor)时,事情开始横向发展。我无法确定要更改颜色的单元格的参考。大概就在眼前,但无论是If/Else还是Case还是Intersect的顺序,我都拿不准。
我已经注释掉了我之前工作的一些方向。
感谢任何帮助,谢谢!
Sub RangeBanding()
Dim rw As Range
Dim col As Range
Dim rng As Range
Dim cell As Range
Set rng = Range("TestRange")
' For each row in range,if even band color
For Each rw In rng.Rows
If Not IsOdd(rw.Row) Then rw.Interior.Color = RGB(241, 241, 241)
Next rw
' For each column in range, if even band color
For Each col In rng.Columns
If Not IsOdd(col.Column) Then col.Interior.Color = RGB(241, 241, 241)
Next col
End Sub
Sub IntersectColor()
Set rng = Range("TestRange")
For Each cell In rng
' cell select to watch step in debug
cell.Select
On Error Resume Next
If cell.Offset.Interior.Color = xlNone Then
cell.Interior.Color = xlNone
ElseIf (cell.Interior.Color = RGB(241, 241, 241)) And _ (cell.Offset(0, -1).Interior.Color = xlNone) Then
cell.Interior.Color = RGB(241, 241, 241)
ElseIf (cell.Interior.Color = RGB(241, 241, 241)) And _ (cell.Offset(-1, -1).Interior.Color = RGB(241, 241, 241)) Then
cell.Interior.Color = RGB(217, 217, 217)
End If
'Select Case cellcolor
'Case Is = (ActiveCell.Interor.Color = RGB(241, 241, 241)) And (ActiveCell.Offset(1, 1).Interior.Color = xlNone)
' ActiveCell.Interior.Color = RGB(217, 217, 217)
'End Select
Next cell
End Sub
Function IsOdd(ByVal l As Long) As Boolean
IsOdd = l Mod 2
End Function
想要的效果: Color intersect Example
【问题讨论】:
-
如果一个单元格是浅色的,而它的邻居是,那么邻居应该是深色的吗?关闭
On Error以开始使用,以便查看是否有任何错误。如果您在第 1 列,则不能向左偏移,这样会导致错误。
标签: vba excel formatting