应用程序启动时VBA类型不匹配
我在使用显示对象触发sub时遇到一些问题。显示对象的结果要么为真,要么为假,我使用_Change方法。代码非常简单应用程序启动时VBA类型不匹配,vba,excel,Vba,Excel,我在使用显示对象触发sub时遇到一些问题。显示对象的结果要么为真,要么为假,我使用_Change方法。代码非常简单 Private Sub clamshellLblRequest_Change() If Not tagDisplay Is Nothing Then GoTo execute Else Set tagDisplay = LoadedDisplays GoTo execute End If execute:
Private Sub clamshellLblRequest_Change()
If Not tagDisplay Is Nothing Then
GoTo execute
Else
Set tagDisplay = LoadedDisplays
GoTo execute
End If
execute:
If clamshellLblRequest.Value = 1 Then
LogDiagnosticsMessage "Requesting clamshell label information"
Call labels.clamshell
End If
End Sub
当我第一次启动应用程序时,我得到一个特定于此值的“类型不匹配”错误(13)。我有几个其他的显示对象,我用同样的方式使用相同的数据类型,但似乎没有这个问题。还有什么原因可能导致这种情况
更新:
我有一个使用标准计时器的模块,包括以下内容
Public Sub tenthSec()
'Create a program delay, DateTime Timer resolution in MSWindows is 0.01. Needed for tag updates.
t = Timer
While Timer - t < 0.1
Wend
End Sub
我不认为这是一个解决办法,也许是一个创可贴。有什么想法吗?同意@Masoud关于等待的看法。您还可以在循环中使用DoEvents,这样可以让其他东西继续计算,等等。此外,您不需要执行:和转到。使用您拥有的代码,您应该能够执行类似的操作(注意,将Not Is Nothing更改为IsNothing):
在哪里定义变量?类型不匹配源于此,我们无法看到它。
clamshellblrequest.Value
是对象值。它不是从VBA本身中定义的。它是布尔型的。不要为wait编写Sub
,而是这样做:Application.wait(现在+#0:00:01#))
这会给代码增加一秒钟的等待时间。我不明白如果clamshellblrequest.Value=1,那么的部分是什么,因为如果Value
是boolean
类型,那么条件是什么。您确定值
是布尔值
?此代码关联的软件是专有的。它将不接受1
为True
。这让我陷入了一个很长时间的循环,因为我不明白为什么我的代码在随机的地方被破坏了。基本上,当我与外部软件交互时,我需要使用1
和0
,而不是True
和False
。这似乎是合乎逻辑的。我将重复使用的任务(不一定在一个软件中重复使用,但在多个软件中频繁使用)保存在模块中,这样我就可以轻松地将它们转储并访问它们。
...
execute:
Call timers.tenthSec
If clamshellLblRequest.Value = 1 Then
LogDiagnosticsMessage "Requesting clamshell label information"
Call labels.clamshell
End If
End Sub
Private Sub clamshellLblRequest_Change()
If tagDisplay Is Nothing Then
Set tagDisplay = LoadedDisplays
End If
Application.Wait(Now + #0:00:01#)
' or
For i = 1 to 1000
DoEvents
Next i
If clamshellLblRequest.Value = 1 Then
LogDiagnosticsMessage "Requesting clamshell label information"
Call labels.clamshell
End If
End Sub