Excel VBA宏:
我想将一年中的日期按季度分割(使用一些颜色) 现在我有一个宏只有两年,但我想让它自动为每年。(例如:红色:在日期01/01/YYYY-01/05/YYYY…之间,以此类推) 这就是我们所做的:Excel VBA宏:,vba,excel,date,Vba,Excel,Date,我想将一年中的日期按季度分割(使用一些颜色) 现在我有一个宏只有两年,但我想让它自动为每年。(例如:红色:在日期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:=
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
结果是:
在代码开头添加
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"
到
在代码的开头添加
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"
到
啊-在这种情况下,您需要:
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
啊-在这种情况下,您需要:
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
谢谢你的回复。但我需要为我添加的一年中的每个数据制作这个宏,我有20152016的数据。。。。2020+. (我不想只呆一年)谢谢你的回复。但我需要为我添加的一年中的每个数据制作这个宏,我有20152016的数据。。。。2020+. (我不想只呆一年)