Excel VBAmacros:
我想每季度分一次(有些颜色)。
现在我只有2年的macros,但是我想每年都会自动做出来。 (例如:红色:01/01 / YYYY-01/05 / YYYY …..等等)
这是做了什么:
Private Sub CheckBox1_Click() If CheckBox1.Value = True Then Rows("1:1").Select Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _ Formula1:="01/01/2015", Formula2:="01/05/2015" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorLight2 .TintAndShade = 0.799981688894314 End With Rows("1:1").Select Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _ Formula1:="30/04/2015", Formula2:="01/08/2015" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .Color = 7461287 .TintAndShade = 0 End With Rows("1:1").Select Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _ Formula1:="31/07/2015", Formula2:="01/01/2016" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .Color = 6750207 .TintAndShade = 0 End With Rows("1:1").Select Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _ Formula1:="01/01/2016", Formula2:="01/05/2016" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorLight2 .TintAndShade = 0.799981688894314 End With Rows("1:1").Select Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _ Formula1:="30/04/2016", Formula2:="01/08/2016" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .Color = 7461287 .TintAndShade = 0 End With Rows("1:1").Select Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _ Formula1:="31/07/2016", Formula2:="01/01/2017" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .Color = 6750207 .TintAndShade = 0 End With End Sub
结果: 在这里input图像描述
啊 – 这种情况你需要这个:
Sub demo() Dim r As Range For Each r In ActiveSheet.UsedRange.Columns(1).Cells If IsDate(r) Then Select Case Month(r) Case 1 To 3 With r.Interior .ThemeColor = xlThemeColorLight2 .TintAndShade = 0.799981688894314 End With Case 4 To 6 r.Interior.Color = 7461287 Case 7 To 8 r.Interior.Color = 7461287 Case 9 To 12 r.Interior.Color = 6750207 End Select End If Next r End Sub
在代码的开始处添加
Private Sub CheckBox1_Click() Const DesiredYear = "2017" 'change this for each year you want
然后改变这样的行:
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _ Formula1:="01/01/2015", Formula2:="01/05/2015"
至
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _ Formula1:="01/01/" & DesiredYear, Formula2:="01/05/" & DesiredYear