我的 Excel 项目在家中可以正常运行(带有Excel 2010),但不能在两台工作计算机上(使用Excel 2016)并且我怀疑Worksheet_Change
事件就是问题。
当用户进行更改时,黄色条(屏幕截图中)应再次变为白色,但事实并非如此。我在两台工作计算机上收到 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
导致此问题的因素可能有很多。一种诊断方法是按如下方式进行故障排除:
在程序开始时,紧接此行之后:
Private Sub Worksheet_Change(ByVal Target As Range)
...添加一个暂时的 line:
MsgBox "Changed: " & Target.Address
...然后去更改工作表中的某些内容(无论发生什么更改都不会按照您的预期触发事件)。
将会发生以下两件事之一:
您将弹出一个消息框,显示刚刚更改的单元格引用。
这表明该事件正在正确触发,因此问题一定出在您后面的代码中。
-
或者,您不会弹出消息框。这表明该事件是not火灾,这可能是由以下几种可能性引起的:
工作簿中的宏是否完全禁用?这通常是在从外部来源收到的工作簿上自动完成的。将工作簿保存到本地计算机或网络上的受信任位置(而不是从电子邮件中打开)。其他部分的代码运行正常吗?当您关闭/重新打开文件时,是否会收到有关宏安全性的警告?另外,尝试重新启动计算机。
-
Other security settings could be an issue. Have you ever run VBA on these machines? You can confirm sure code is able to run in Excels' security settings in:
File→Options→Trust Center→Trust Center Settings→Macro Settings
As well as making sure macros are enabled there, you could also check Trusted Locations in the Trust Center, and either save your document in a listed location, or add a new location. Security settings will be "reduced" for documents saved in those locations.
Is EnableEvents
在代码中的其他地方被故意禁用?如果你写了所有的代码,你应该知道你是否设置了EnableEvents = False
在某一点。也许这是故意的,但它没有被重新启用。
请记住删除临时添加的行,否则每次进行更改时,MsgBox 都会弹出,很快就会变得烦人。 :)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)