2015-09-26 42 views
1

例如:“与”条件评估,即使以前的状态是虚假的,VBA

Dim name As String: name = "NaN" 

' Doesn't generate an error: 
If IsNumeric(name) Then 
    Dim iv As Integer: iv = CInt(name) 
    If iv > 0 Then 
     ' Do something 
    End If 
End If 

' Does generate error 13 on 'CInt(name)': Types don't match 
If IsNumeric(name) And CInt(name) > 0 Then 
    ' Do something 
End If 

为什么第二个条件CInt(name) > 0即使单if语句评价?或者这正是VBA所做的事情?我习惯于编写没有这种行为的C#代码。

+3

请参阅[vba短路逻辑](http://stackoverflow.com/questions/7015471/does-the-vba-and-operator-evaluate-the-second-argument-when-the-first-is-false ) – amdixon

回答

2

或者这正是VBA所做的?

是。

使用这个代替:

If IsNumeric(name) Then 
    If CInt(name) > 0 Then 
     ' Do something 
    End If 
End If 

或由amdixon链接的post任何其他方法。

+0

我明白了。太糟糕了,你不能在Excel中使用C#来编写宏:( –