vba - 按颜色突出显示相交

标签 vba excel formatting

我正在处理一些自定义格式、带状行和列以及它们相交的位置,突出显示为较暗的阴影。

两个程序一起工作。第一个(RangeBanding)按预期工作,并对偶数行和列进行绑定(bind)。

当我运行第二个(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

最佳答案

多一个:

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

关于vba - 按颜色突出显示相交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44529777/

相关文章:

excel - 动态创建的表单控件标签上的 MouseMove 事件

excel - 需要了解 Row() 函数在 SUMPRODUCT 下是如何工作的

xml - 使用 R 将新数据附加到格式化的 xlsx 工作表

java - 变量类型问题

vba - 在 Excel 中使用 QueryTables 导入带引号换行符的 csv

mysql - 将 Excel CSV 导入 MySQL 关系数据库?

forms - 如何在 MS Access VBA 中检索屏幕大小/分辨率以调整表单大小

c - 如何将 .c 文件从 Windows 重新格式化为 UNIX?

Excel VBA : chr-function error when using value > 255

excel - 自动安装 Excel vba 插件