【发布时间】:2018-09-13 11:21:04
【问题描述】:
我的 Excel 项目在家中正常运行(使用 Excel 2010),但不能在两台工作计算机上正常运行(使用 Excel 2016),我怀疑 Worksheet_Change 事件是问题。
当用户进行更改时,黄色条(在屏幕截图中)应该再次变为白色,但事实并非如此。我在 2 台工作计算机上收到 2 种不同的响应。
在代码中需要指出两点:
在某些地方我使用
vbColor扩展名,在其他地方我必须使用数字代码。一台计算机根本没有触发
Worksheet_Change事件。我会注意到更改事件位于代码的顶部,尽管这与它没有任何关系。
感谢您的建议和详细解释,以帮助我学习。
Private Sub Worksheet_Change(ByVal Target As Range) 'Check for On-Time and Delays then change the Command Button Colors to show completed.
'Return headers to white after jump to
Range("B3:I3,O3:V3,B28:I28,O28:V28,B53:I53,O53:V53,B78:I78,O78:V78,B103:I103,O103:V103,B128:I128,O128:V128,B153:I153,O153:V153").Interior.Color = vbWhite
'Check for On Time and Delayed Trips
'Trip 1 Scan Ready
If IsEmpty(Range("L3").Value) = False Then
If Range("L3").Value > Range("I3").Value Then 'If actual is greater than Departure
'If Delayed check for a delay code
If IsEmpty(Range("L24").Value) Then 'If Delay code is missing
Range("K24:L25").Interior.Color = 16711935
CommandButton1.BackColor = 16711935
CommandButton1.ForeColor = vbBlack
Else 'If Delay Code is present check for delay time
If IsEmpty(Range("L25").Value) Then
Range("K24:L25").Interior.Color.Index = 16711935
CommandButton1.BackColor = 16711935
CommandButton1.ForeColor = vbBlack
Else
CommandButton1.BackColor = vbRed
CommandButton1.ForeColor = vbWhite
Range("K24:L25").Interior.Color = vbWhite
End If
End If
Else
'Flight was on Time
CommandButton1.BackColor = 32768 '32768 = Green
CommandButton1.ForeColor = vbWhite
Range("K24:L25").Interior.Color = vbWhite
End If
End If
【问题讨论】:
-
not firing the Worksheet_Change可能是安全设置,颜色尝试使用 Excel RGB 颜色示例.Color = RGB(0, 0, 0) -
^ 对于触发事件,请尝试运行此行
Application.EnableEvents = True,对于白条,在行If Range("L3").Value > Range("I3").Value上放置一个断点并检查两个单元格的值(和时间格式) -
保罗,我在第一次活动之前把那句话放进去吗。
-
像这样创建一个单独的 Sub:
Sub ee(): Application.EnableEvents = True: End Sub,然后按 F5 -
@pica - 同样,他不得不在代码中的某个位置禁用事件;我不明白是什么让你相信他这样做了?我不是要争论——只是好奇是什么导致了这种“预感”。另外 - 一个快速确认的方法是
[CTRL]+[G]然后?Application.EnableEvents和[Enter]。