打开电子表格时,请刷新所有VBA代码

时间:2018-05-21 21:32:12

标签: excel vba excel-vba

我是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

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

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

提前致谢: - )

1 个答案:

答案 0 :(得分:2)

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

sqoop-import -Dmapreduce.job.user.classpath.first=true --connect "jdbc:sqlserver://stuff:1433;database=prd_swift_core;user=username;password=red123:" --table MYTABLE -m 1 --as-avrodatafile --target-dir s3n://bucket/folder1/folder2/MYTABLE/DATE

然后将此事件处理程序例程添加到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