You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:打开Excel表格时自动刷新VBA标签变色代码

Excel VBA: Worksheet Tab Color Won't Auto-Refresh When Opening the Workbook Daily

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:03:34