日期格式无法识别

时间:2019-12-12 14:36:19

标签: excel vba date formatting

我正在尝试根据另一个单元格中的日期来格式化包含日期的单元格。该代码似乎有效,但是仅当要格式化的单元格的“ AM”或“ PM”与参考单元格匹配时。因此,举例来说,如果参考储存格(在这个范例中为AA11)的date:time为12/12/2019 8:00:00 AM,那么只有+ 0,+ 1,+ 2等天的储存格“ AM”后缀将被格式化。 + 0,+ 1,+ 2等天且后缀为“ PM”的单元格不会被格式化。我需要添加任何代码来识别AM和PM吗?

`Sub ChangeColor()
myDate = Range("AA11").Value
Set MR = Range("$A2:$BU300")
For Each Cell In MR
    If (Cell.Value) = myDate Then Cell.Interior.ColorIndex = 44
    If (Cell.Value) = DateAdd("d", 1, myDate) Then Cell.Interior.ColorIndex = 5
    If (Cell.Value) = DateAdd("d", 2, myDate) Then Cell.Interior.ColorIndex = 77
    If (Cell.Value) = DateAdd("d", 3, myDate) Then Cell.Interior.ColorIndex = 3
    If (Cell.Value) = DateAdd("d", 4, myDate) Then Cell.Interior.ColorIndex = 32
    If (Cell.Value) = DateAdd("d", 5, myDate) Then Cell.Interior.ColorIndex = 24
    If (Cell.Value) = DateAdd("d", 6, myDate) Then Cell.Interior.ColorIndex = 31
    If (Cell.Value) = DateAdd("d", 7, myDate) Then Cell.Interior.ColorIndex = 15
        Next
       Range("W4").Select
    ActiveWindow.SmallScroll Down:=-300
End Sub`

0 个答案:

没有答案
相关问题