【问题标题】:VBA highlight cell that is not a date in a rangeVBA突出显示不是范围内日期的单元格
【发布时间】:2018-06-19 14:45:39
【问题描述】:

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

 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 屏幕截图

【问题讨论】:

    标签: vba date highlight


    【解决方案1】:

    或者

    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
    

    【讨论】:

      【解决方案2】:

      使用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

      【讨论】:

        猜你喜欢
        • 2022-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-04
        • 1970-01-01
        • 2020-05-19
        相关资源
        最近更新 更多