我有一个表格设置了这样的内容:VBA条件格式基于单元格合并
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)被删除时,收到一个错误(运行时错误“13”:类型Mistmatch)就行了“案‘S’ ” 。我可以用'On Error GoTo'这一行来解决它,但这意味着已经有条件格式化的单元格不会返回到'不填充'。如果它是在未合并的单元格上完成的,那么这不是问题,所以可能需要我们不再一起使用合并的单元格。但是,对于用户友好性来说,保留它会很好(而不是使用户在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
你在哪一行得到错误? – assylias
为什么不运行VBA来查找合并的单元格,要么跳过它们,要么重置colorIndex以使其没有颜色... – bonCodigo
@assylias - 错误指向宏中的“Case”S“'。谢谢。@ bonCodigo - 输入发生在合并的单元格中,所以我不认为我想跳过它们。我认为caseelse应该工作(并且在非合并单元格上工作),所以我不知道如何去做你的建议。谢谢。 – bawpie