【发布时间】:2020-01-25 09:38:59
【问题描述】:
我在 Excel 中有一个日历,一直持续到 2020 年 7 月。我希望在我的工作表中拥有的是每次打开工作簿时自动着色“当前月份”的所有单元格。
我已经做了“当天自动着色” 这是代码:
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(cell) = Month(Date) then这样你就可以选择月份。 -
类似:
If cell.Value = Month(Date) Then cell.Interior.Color = 'Color End If -
不,因为
cell.value是日期,而不是月份...您需要使用Month()进行两个比较。看看下面的答案。