基于合并单元格内容的VBA条件格式

时间:2012-11-22 12:20:37

标签: vba merge excel-2003 conditional-formatting

我的电子表格设置如下:

1   Basic Rota  09:00   13:00
2   Absence           S 

如果您想象列标签从上面开始' Basic Rota'作为A,B和C.缺席单元格(B2:C2)是合并的单元格,其可以包含' H' S'' T', ' SC'或者它可以是空的。根据该单元格的内容,B1和C1应该改变颜色。我有一点VBA可以完成这项工作。

Option Compare Text 'A=a, B=b, ... Z=z
Option Explicit


Private Sub Worksheet_Change(ByVal Selection As Range)

        Select Case Target.Value

    Case "S"

        Target.MergeArea.Offset(-1, 0).Interior.ColorIndex = 53
        Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = 53
        Target.MergeArea.Offset(-1, 0).Font.ColorIndex = 53
        Target.MergeArea.Offset(-1, 1).Offset(0, -1).Font.ColorIndex = 53

    Case "H"

        Target.MergeArea.Offset(-1, 0).Interior.ColorIndex = 50
        Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = 50
        Target.MergeArea.Offset(-1, 0).Font.ColorIndex = 50
        Target.MergeArea.Offset(-1, 1).Offset(0, -1).Font.ColorIndex = 50

    Case "T"

        Target.MergeArea.Offset(-1, 0).Interior.ColorIndex = 44
        Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = 44
        Target.MergeArea.Offset(-1, 0).Font.ColorIndex = 44
        Target.MergeArea.Offset(-1, 1).Offset(0, -1).Font.ColorIndex = 44

    Case "SC"

        Target.MergeArea.Offset(-1, 0).Interior.ColorIndex = 42
        Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = 42
        Target.MergeArea.Offset(-1, 0).Font.ColorIndex = 42
        Target.MergeArea.Offset(-1, 1).Offset(0, -1).Font.ColorIndex = 42

    Case Else

        Target.Offset(-1, 0).Interior.ColorIndex = xlNone 'No Fill
        Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = xlNone

End Select

End Sub

但是,如果合并单元格(B2:C2)的内容被删除,我会收到错误(运行时错误' 13':Type Mistmatch)' Case& #34; S" &#39 ;.我可以通过“On Error GoTo' line,但这意味着已经有条件格式化的单元格不会返回“没有填充”。如果它在未合并的单元格上完成,这不是问题,因此我可能需要停止使用合并的单元格 - 但是,为了用户友好性,它可能会出现问题。很高兴保持它(而不是让用户输入' H'例如在B2和C2中两次)。作为参考,这适用于Excel 2003.我应该通过查看该工作表的代码添加宏添加到工作表中,并基于worksheet_change。

如果有人可以提供帮助,我们将非常感激!

编辑:根据@Philip A Barnes'答案。

  Private Sub Worksheet_Change(ByVal Target As Range)


  Select Case Target.Columns(1).Value

  Case Empty

    Target.Columns(1).MergeArea.Offset(-1, 0).Interior.ColorIndex = xlNone 'No Fill
    Target.Columns(1).MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = xlNone

Case "S"

    Target.MergeArea.Offset(-1, 0).Interior.ColorIndex = 53
    Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = 53
    Target.MergeArea.Offset(-1, 0).Font.ColorIndex = 53
    Target.MergeArea.Offset(-1, 1).Offset(0, -1).Font.ColorIndex = 53

Case "H"

    Target.MergeArea.Offset(-1, 0).Interior.ColorIndex = 50
    Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = 50
    Target.MergeArea.Offset(-1, 0).Font.ColorIndex = 50
    Target.MergeArea.Offset(-1, 1).Offset(0, -1).Font.ColorIndex = 50

Case "T"

    Target.MergeArea.Offset(-1, 0).Interior.ColorIndex = 44
    Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = 44
    Target.MergeArea.Offset(-1, 0).Font.ColorIndex = 44
    Target.MergeArea.Offset(-1, 1).Offset(0, -1).Font.ColorIndex = 44

Case "SC"

    Target.MergeArea.Offset(-1, 0).Interior.ColorIndex = 42
    Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = 42
    Target.MergeArea.Offset(-1, 0).Font.ColorIndex = 42
    Target.MergeArea.Offset(-1, 1).Offset(0, -1).Font.ColorIndex = 42

Case Else

    Target.Offset(-1, 0).Interior.ColorIndex = xlNone 'No Fill
    Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = xlNone

End Select

End Sub

1 个答案:

答案 0 :(得分:0)

这是因为当单元格中没有数据时,Target引用返回“Empty”。您需要扩展您的案例陈述以检查:

Private Sub Worksheet_Change(ByVal Target As Range)


Select Case Target.Columns(1).Value

    Case Empty

        Target.Offset(-1, 0).Interior.ColorIndex = xlNone 'No Fill
        Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = xlNone

    Case "S"

        Target.MergeArea.Offset(-1, 0).Interior.ColorIndex = 53
        Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = 53
        Target.MergeArea.Offset(-1, 0).Font.ColorIndex = 53
        Target.MergeArea.Offset(-1, 1).Offset(0, -1).Font.ColorIndex = 53

    Case "H"

        Target.MergeArea.Offset(-1, 0).Interior.ColorIndex = 50
        Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = 50
        Target.MergeArea.Offset(-1, 0).Font.ColorIndex = 50
        Target.MergeArea.Offset(-1, 1).Offset(0, -1).Font.ColorIndex = 50

    Case "T"

        Target.MergeArea.Offset(-1, 0).Interior.ColorIndex = 44
        Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = 44
        Target.MergeArea.Offset(-1, 0).Font.ColorIndex = 44
        Target.MergeArea.Offset(-1, 1).Offset(0, -1).Font.ColorIndex = 44

    Case "SC"

        Target.MergeArea.Offset(-1, 0).Interior.ColorIndex = 42
        Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = 42
        Target.MergeArea.Offset(-1, 0).Font.ColorIndex = 42
        Target.MergeArea.Offset(-1, 1).Offset(0, -1).Font.ColorIndex = 42

    Case Else

        Target.Offset(-1, 0).Interior.ColorIndex = xlNone 'No Fill
        Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = xlNone

End Select

End Sub

确保这是您的第一次检查。另外,我建议您查看使用VBA操作的条件格式内置的Excel。