【问题标题】:Refresh all VBA Codes when opening Spreadsheet打开电子表格时刷新所有 VBA 代码
【发布时间】:2018-05-22 05:44:46
【问题描述】:

我是 Excel 中的 VBA 新手,但在我的电子表格中编写了几个不同的代码,这些代码在测试时完全符合要求。我的两个代码都会根据日期和距离发生某事的天数更改选项卡的颜色。这是一个例子:

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

唯一的问题是,当我打开电子表格时,上面的代码不会每天刷新。选项卡颜色仍然保持在我插入和测试代码时的状态,并且不会随着“今天”日期的变化而每天变化。随着时间的推移,标签颜色应从标准颜色变为橙色,再变为红色。

谁能提供一些帮助?我在电子表格的另一个选项卡上也有类似的代码,有同样的问题。非常感谢新手的任何帮助!

提前致谢:-)

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    只需将您的事件处理程序例程转为接受工作表对象作为参数的普通子程序:

    Private Sub ColorSheets(sht As Worksheet)
    
        Dim i As Integer
        Dim daysLeft As Integer
        daysLeft = 100 'just a number larger than you need
    
        With sht
            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
                    .Tab.ColorIndex = xlColorIndexNone
                Case 1 To 5
                    .Tab.ColorIndex = 45
                Case 0
                    .Tab.ColorIndex = 3
            End Select
        End With
    
    End Sub
    

    然后将此事件处理程序例程添加到您的ThisWorkbook 代码窗格:

    Private Sub Workbook_Open()
        Dim sh As Worksheet
        For Each sh In Worksheets ' loop through worksheets
            ColorSheets sh ' call your routine passing current sheet
        Next
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多