我正在运行一个宏,在该宏中,代码首先需要清除该范围中单元格的所有颜色填充,然后在其中正在检查该单元格中的所有数据以为其提供正确颜色的地方运行代码。每次打开excel文件时都需要运行此宏。
我目前有:
Sub ChangeColor()
Dim rCell As Range
With Sheet1
For Each rCell In .Range("D5:M21") ' all cells in the table
If rCell.Row Mod 2 = 1 Then ' Only odd rows
If rCell.Value <> "" Then ' if there is a value in a cell then
If rCell.Value <= Date Then 'if cell date is today or in the past then
rCell.Interior.Color = vbRed ' make the cell red
ElseIf rCell.Value <= Date + 30 Then ' else if the date in the cell is between now and 30 days then
rCell.Interior.Color = RGB(255, 150, 0) ' make the cell orange
ElseIf rCell.Value <= Date + 90 Then ' else if the cell is between 30 en 90 days from date today then
rCell.Interior.Color = vbYellow ' make the cell yellow
End If
End If
End If
Next rCell
End With
End Sub
所以我知道我需要但下面类似的内容,但是不知道如何成功将其转换为我的代码。当我按自己的方式尝试时,所有单元格都空白。
.Cells.Interior.Pattern = xlNone