代码在其他系统上的表现不同

时间:2014-05-05 12:59:13

标签: excel vba

这是我的L.O.C,它在其他系统上的工作方式不同。此代码是在VBA Excel中编写的,并在PowerPoint文件上执行搜索操作。

Set oTmpRng = oTxtRng.Find( _
    FindWhat:=searchtext, _
    WholeWords:=False, _
    matchcase:=matchvalue)

这里,如果在PowerPoint文件中不存在searchtext中的单词,那么它在我的系统中返回“Nothing”,但同样在我的同事的系统中返回空白(“”)。还有一件事,oTmpRng =在我的系统中没有任何东西正在返回,在这种情况下,下面的代码行不应该执行。但它仍然在里面。

If Not oTmpRng Is Nothing and oTmpRng <> "" Then
     msg "It should not execute" '<- This line should not execute when oTmpRng=Nothing
End If

注意:两个系统都包含Office 2007。 任何人都可以让我知道为什么会这样。

1 个答案:

答案 0 :(得分:1)

你在代码中的某个地方有错误恢复吗?如果条件中存在错误,则可能导致始终执行。

否则,Not的优先级最低,所以我总是使用括号, 要尝试的东西:

If (Not oTmpRng Is Nothing) and oTmpRng <> "" Then
     msg "It should not execute" '<- This line should not execute when oTmpRng=Nothing
End If

If Not (oTmpRng Is Nothing and oTmpRng = "") Then
         msg "It should not execute" '<- This line should not execute when oTmpRng=Nothing
    End If