应用程序启动时VBA类型不匹配

时间:2017-04-27 15:09:34

标签: vba excel-vba excel

我在用于触发sub的显示对象时遇到了一些问题。显示对象的结果为true或false,我使用_Change方法。代码非常简单。

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

当我第一次启动应用程序时,我得到了#34;类型不匹配"错误(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

当我在评估对象的值之前执行调用timers.tenthSec时,它似乎没有抛出类型不匹配。

...    
execute:
        Call timers.tenthSec
        If clamshellLblRequest.Value = 1 Then
            LogDiagnosticsMessage "Requesting clamshell label information"
            Call labels.clamshell
        End If
End Sub

我不会称这是一个解决方案,也许是一个创可贴。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

同意@Masoud关于等待。你也可以在循环中使用DoEvents,这允许其他东西继续计算,等等。另外,你不应该执行:并且转到你拥有的代码,你应该能够做这样的事情(注意改变不是什么都没有):

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