0

我有一个这样的电子表格:

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

如果您想象列标签从“基本轮换”上方开始为 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) 的内容被删除,我会在 'Case "S" ' 行收到错误(运行时错误 '13': Type Mistmatch)。我可以通过“On Error GoTo”行绕过它,但这意味着已被条件格式化的单元格不会返回“no fill”。如果它是在未合并的单元格上完成的,这不是问题,所以我可能需要停止一起使用合并的单元格 - 但是,为了用户友好,保留它会很好(而不是制作例如,用户在 B2 和 C2 中输入“H”两次)。作为参考,这适用于 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
4

1 回答 1

0

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

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

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

于 2012-11-22T13:00:53.823 回答