【问题标题】:Highlight Intersect By Color按颜色突出显示相交
【发布时间】: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


【解决方案1】:

还有一个:

Option Explicit

Public Sub RangeBanding()
    Dim itm As Range, isEven As Boolean, isXing As Boolean
    Dim clr1 As Long, clr2 As Long, clrW As Long, clr As Long

    clr1 = RGB(241, 241, 241)   'light
    clr2 = RGB(217, 217, 217)   'dark
    clrW = xlNone               'transparent (white)

    Application.ScreenUpdating = False
    For Each itm In ThisWorkbook.Sheets(1).Range("TestRange").Cells
        With itm
            isEven = .Row Mod 2 = 0 Or .Column Mod 2 = 0
            isXing = .Row Mod 2 = 0 And .Column Mod 2 = 0
            clr = clrW
            Select Case True
                Case isXing: clr = clr2 'must be first in the select statement
                Case isEven: clr = clr1
            End Select
            .Interior.Color = clr
        End With
    Next
    Application.ScreenUpdating = True
End Sub

【讨论】:

  • 这非常有效。额外的颜色是一种很好的触感;我将它从白色更新为“xlNone”。一切都顺利进行。谢谢!
【解决方案2】:
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

    For Each cell In rng.Cells
        If Not IsOdd(cell.Column) And Not IsOdd(cell.Row) Then
            col.Interior.Color = RGB(217, 217, 217)
        Next col
    End if

End Sub

【讨论】:

    【解决方案3】:

    一对条件格式规则应该可以解决这个问题。

    With Range("TestRange")
        .FormatConditions.Delete
        With .FormatConditions.Add(Type:=xlExpression, Formula1:="=AND(NOT(MOD(ROW(), 2)),NOT(MOD(COLUMN(), 2)))")
            .Interior.Color = RGB(217, 217, 217)
            .StopIfTrue = True
        End With
        With .FormatConditions.Add(Type:=xlExpression, Formula1:="=OR(NOT(MOD(ROW(), 2)),NOT(MOD(COLUMN(), 2)))")
            .Interior.Color = RGB(241, 241, 241)
            .StopIfTrue = True
        End With
    End With
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-24
      • 2010-12-12
      • 2011-12-09
      • 1970-01-01
      • 2021-12-20
      相关资源
      最近更新 更多