VBA 突出显示不是范围内日期的单元格
VBA highlight cell that is not a date in a range
我试图突出显示范围内的非日期值。但是我的代码以某种方式让我突出显示空白单元格,
可以请教我应该改变什么吗?
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 截图
使用SpecialCells()
方法遍历"constant"(即跳过空白)单元格
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
或
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
我试图突出显示范围内的非日期值。但是我的代码以某种方式让我突出显示空白单元格, 可以请教我应该改变什么吗?
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 截图
使用SpecialCells()
方法遍历"constant"(即跳过空白)单元格
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
或
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