嗨,我无法使此代码块正常工作
Dim rng As Range, cll As Range
Set rng = Range("January") ' I defined custom range with name January
For Each cll in rng
If Month(cll) = Month(A7) Then ' in A7 cell is date with January month in it
cll.Font.ColorIndex = 5
Else
cll.Font.ColorIndex = 10
End If
Next
答案 0 :(得分:1)
用Month(A7)
或Month([A7])
或Month(Range("A7"))
或Month(Cells(7,"A"))
代替Month(Cells(7,1))
答案 1 :(得分:0)
让我们假设数据出现在工作表1中。尝试以下代码:
Option Explicit
Sub TEST()
Dim rngMonth As Range, cell As Range
With ThisWorkbook.Worksheets("Sheet1")
Set rngMonth = .Range("January")
For Each cell In rngMonth
If Month(cell) = Month(.Range("A7")) Then
cell.Font.ColorIndex = 5
Else
cell.Font.ColorIndex = 10
End If
Next
End With
End Sub