在VBA中使用通配符进行条件格式化(Excel 2003)

我在下面的VBA中添加了一堆条件格式到Excel 2003中的工作表(它只允许3种情况),但是它不能正确地使用通配符。 如果我用通配符replace确切的值,它将正确运行。 我怀疑有关<>的最后一个声明可能需要调整,但我不知道如何在最后使用<>跳过LIKE“Wildcard”。 VBA代码如下:

Private Sub Worksheet_Change (ByVal Target As Range) Set MyPlage = Range(“C3:I11,C13:I34”) For Each Cell in MyPlage If Cell.Value Like “A*” Then Cell.Interior.ColorIndex = 38 End If If Cell.Value Like “B*” Then Cell.Interior.ColorIndex = 35 End If If Cell.Value Like “C*” Then Cell.Interior.ColorIndex = 34 End If If Cell.Value Like “D*” Then Cell.Interior.ColorIndex = 40 End If If Cell.Value <> “A*” And Cell.Value <> “B*” And Cell.Value <> “C*” And Cell.Value <> “D*” Then Cell.Interior.ColorIndex = xlNone End If Next End Sub 

你不需要通配符,因为它只是一个基本的If … ElseIf … End If结构:

  Private Sub Worksheet_Change (ByVal Target As Range) Set MyPlage = Range(“C3:I11,C13:I34”) For Each Cell in MyPlage If Cell.Value Like “A*” Then Cell.Interior.ColorIndex = 38 ElseIf Cell.Value Like “B*” Then Cell.Interior.ColorIndex = 35 ElseIf Cell.Value Like “C*” Then Cell.Interior.ColorIndex = 34 ElseIf Cell.Value Like “D*” Then Cell.Interior.ColorIndex = 40 Else Cell.Interior.ColorIndex = xlNone End If Next End Sub