我正在使用一个方便的 VBA 脚本,将硬编码值更改为亮蓝色字体。
它效果很好,除了重新着色我的日期,这些日期通常是深色背景的白色字体(我不希望这些改变)。日期始终使用日期数字格式。有没有办法从以下内容中排除格式化为日期的数字?
Sub FormatCellsBasedOnContent()
'color format macro
Dim myRange As Range
Set myRange = Selection
Selection.SpecialCells(xlCellTypeConstants, xlNumbers).Font.Color = RGB(0, 0, 255) ' Blue
Selection.SpecialCells(xlCellTypeFormulas).Font.Color = RGB(0, 0, 0) ' Black
End Sub
如果这是不可能的,是否有其他方法可以排除这些细胞?例如,是否可以排除任何具有背景颜色的内容或排除 EOMONTH/DATE/EDATE 公式?我尝试过这个,但不起作用。
Sub FormatCellsBasedOnContent()
'color format macro
Dim myRange As Range
Set myRange = Selection
For Each cell In Selection
If Selection.NumberFormat = "m/d/yyyy" Then ''
ElseIf Selection.NumberFormat = "[$-en-US]mmm-yy;@" Then ''
Else
Selection.SpecialCells(xlCellTypeConstants, xlNumbers).Font.Color = RGB(0, 0, 255) ' Blue
Else
Selection.SpecialCells(xlCellTypeFormulas).Font.Color = RGB(0, 0, 0) ' Black
End If
Next
End Sub
试试这个:
Sub FormatCellsBasedOnContent()
Dim cell As Range, v
For Each cell In Selection.Cells
If Not cell.NumberFormat Like "*yy*" Then 'looks like a date?
If cell.HasFormula Then 'has a formula?
cell.Font.Color = vbBlack
Else
v = cell.Value
If Len(v) > 0 And IsNumeric(v) Then 'number?
cell.Font.Color = vbBlue
End If
End If
End If
Next cell
End Sub