2012-11-22 71 views
0

我有一个表格设置了这样的内容: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 
+0

你在哪一行得到错误? – assylias

+0

为什么不运行VBA来查找合并的单元格,要么跳过它们,要么重置colorIndex以使其没有颜色... – bonCodigo

+0

@assylias - 错误指向宏中的“Case”S“'。谢谢。@ bonCodigo - 输入发生在合并的单元格中,所以我不认为我想跳过它们。我认为caseelse应该工作(并且在非合并单元格上工作),所以我不知道如何去做你的建议。谢谢。 – bawpie

回答

0

这是因为当单元格中没有数据时,目标引用返回“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 

确保它是你做的第一个检查。此外,我会建议寻找Excels建立在条件格式,你可以使用VBA操纵。

+0

谢谢,但如果我将它添加到我的case语句的开头,它仍然返回从'Case Empty'开始的相同错误。就我所知,对于这个特定的电子表格,我需要3个以上的条件格式,这在Excel 2003中是不允许的。 – bawpie

+0

看到我的上面编辑完整的代码,我收到没有错误。你如何触发该事件?如果您使用的是2007,则Excel 2003有3个上限,但没有此限制。 – InContext

+0

非常感谢,如果我点击'删除'键,我收到错误。如果我选择单元格的内容并通过公式栏删除它们,则不会发生错误!不幸的是,我们还没有更新到2007年,并且暂时不会。 – bawpie