如何在日历中自动为当月的单元格着色

How to automatically color cells of the current month in calendar

我在 Excel 中有一个日历,一直持续到 2020 年 7 月。我希望在我的工作表中拥有的是每次我打开工作簿时 "current month" 的所有单元格的自动着色.

我已经做了 "current day auto-coloration" 这是代码:

Public Sub FormattaCalendario()

  Dim LColCount As Long
  Dim cell As Variant

  ultimoGiorno = DateSerial(Year(Date), Month(Date), 0)
  primoGiorno = ultimoGiorno - Day(ultimoGiorno) + 1

  LColCount = Cells(TrovaInizioProgetti(activeCell) - 1, 
  Columns.Count).End(xlToLeft).Column

  For Each cell In Range(Cells(TrovaInizioProgetti(activeCell) - 1, 11), 
  Cells(TrovaInizioProgetti(activeCell) - 1, LColCount))

    If cell.Value = Date Then
      cell.Interior.Color = RGB(255, 255, 91)
    End If

    If CDate(cell.Value) <> Date Then
      cell.Interior.Color = RGB(255, 150, 0)
    End If

  Next

End Sub

提前致谢。

此替换应该可以满足您的要求:

If Month(CDate(cell.Value)) = Month(Date) Then
  cell.Interior.Color = RGB(255, 255, 91)
Else
  cell.Interior.Color = RGB(255, 150, 0)
End If

请注意,您不需要两个单独的 IF 声明。

使用条件格式可以更轻松地完成此操作。将此公式应用于您的日期所在的范围:

=MONTH(A1)=MONTH(NOW())

这将突出显示该范围内日期在当前月份内的每个单元格。确保将 "A1" 更改为该范围内的第一个单元格。或者,如果您想要突出显示整行,请改为输入 $*Column*1,其中 *Column* 是您的日期所在的列。然后应用于整个 sheet.

EDIT 因为我仍然相信这个答案而不是 VBA 选项,所以我还将回答对该答案的评论中的问题:

今天可以用公式突出显示:

=A1=TODAY()

编辑 2 突出周末的最佳公式如下:

=IF(CELL("format",A1)="D1",WEEKDAY(A1,2)>5,FALSE)

感谢@shrivallabha.redij 和@Foxfire 和 Burns 和 Burns