求助:打开Excel表格时自动刷新VBA标签变色代码
Hi everyone, I'm new to Excel VBA and I've written two pieces of code that change worksheet tab colors based on dates and remaining days. When I test them manually (by editing cells), they work perfectly. Here's one of the code examples:
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
The problem I'm facing is: this code doesn't automatically refresh when I open the workbook each day. The tab color stays stuck in whatever state it was in during testing, and doesn't update to reflect the current date (it should shift from default to orange, then red as the due date approaches). The other worksheet with similar code has the exact same issue.
I'd really appreciate any help or advice you can offer to fix this!
内容的提问来源于stack exchange,提问作者RabHillcrest

