如果月份值相同,则设置font.color

时间:2018-12-31 08:56:06

标签: excel vba excel-vba

嗨,我无法使此代码块正常工作

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

2 个答案:

答案 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