VBA突出显示不是范围中日期的单元格

时间:2018-04-23 09:43:42

标签: vba date highlight

我试图突出显示范围内的非日期值。但是我的代码以某种方式让我突出显示空白单元格, 请告诉我应该改变什么?

 Sub colortest()
 Set MyPage = Range("B2:D6")
 For Each cell In MyPage
 Select Case cell.Value
 Case Not IsDate(cell) = False
 cell.Interior.Color = 65535
 Case Is = "abc"
 cell.Interior.ColorIndex = 15
 End Select
 Next
 End Sub

Excel屏幕截图

Excel screen shot

2 个答案:

答案 0 :(得分:0)

使用SpecialCells()方法循环“常量”(即跳过空白)单元格

Dim MyPlage As Range, cell As Range

Set MyPlage = Range("B2:D6")
For Each cell In MyPlage.SpecialCells(xlCellTypeConstants)
    Select Case cell.Value
        Case Not IsDate(cell) = False
            cell.Interior.Color = 65535
        Case Is = "abc"
            cell.Interior.ColorIndex = 15
    End Select
Next

如果MyPlage范围内的数据包含公式,则只需将xlCellTypeConstants更改为xlCellTypeFormulas

答案 1 :(得分:0)

Option Explicit
Public Sub colortest()
    Dim MyPlage As Range, currentCell As Range
    Set MyPlage = Range("B2:D6")

    For Each currentCell In MyPlage
        If Not IsEmpty(currentCell) Then
            Select Case IsDate(currentCell.Value)
            Case 1
                currentCell.Interior.Color = 65535
            Case 0
                currentCell.Interior.Color = 15
            End Select
        End If
    Next currentCell
End Sub