我正在尝试根据同一张表格中生成的单元格值更改工作表的标签颜色(单元格值实际上是显示付款到期时的文本,您将在下面的代码中看到)。所有文本值都通过单元格“C6:C29”中的公式生成。
我编写的代码完全符合我的要求,但目前只适用于单元格“C6”。当我尝试将我的代码中的“C6”更改为“C6:C29”时,它不再起作用(抛出错误)。
任何人都可以建议如何扩展我的代码以检查单元格范围“C6:C29”而不仅仅是单个单元格“C6”?
Private Sub Worksheet_Change(ByVal Target As Range)
Select Case Range("C6").Value
Case "Due In 5 Days"
Me.Tab.ColorIndex = 45
Case "Due In 4 Days"
Me.Tab.ColorIndex = 45
Case "Due In 3 Days"
Me.Tab.ColorIndex = 45
Case "Due In 2 Days"
Me.Tab.ColorIndex = 45
Case "Due Tomorrow"
Me.Tab.ColorIndex = 45
Case "Due Today"
Me.Tab.ColorIndex = 3
Case Else
Me.Tab.ColorIndex = xlColorIndexNone
End Select
End Sub
非常感谢任何帮助。
答案 0 :(得分:0)
我假设你想要的东西:
如果今天至少有一个到期,请将其设为ColorIndex = 3,
如果在5天内至少有一个到期,但今天没有到期,那么将其设为45,
如果没有更进一步,请将其设为默认颜色。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Integer
Dim daysLeft As Integer
daysLeft = 100 'just a number larger than you need
For i = 6 To 29
Select Case Range("C" & i).Value
Case "Due In 5 Days"
If daysLeft >= 5 Then daysLeft = 5
Case "Due In 4 Days"
If daysLeft >= 4 Then daysLeft = 4
Case "Due In 3 Days"
If daysLeft >= 3 Then daysLeft = 3
Case "Due In 2 Days"
If daysLeft >= 2 Then daysLeft = 2
Case "Due Tomorrow"
If daysLeft >= 1 Then daysLeft = 1
Case "Due Today"
If daysLeft >= 0 Then daysLeft = 0
End Select
Next
Select Case daysLeft
Case 100
Me.Tab.ColorIndex = xlColorIndexNone
Case 1 To 5
Me.Tab.ColorIndex = 45
Case 0
Me.Tab.ColorIndex = 3
End Select
End Sub