代码之家  ›  专栏  ›  技术社区  ›  Berryl

当arg为空与为零时,我是否可以强制VBA以不同的方式处理变量arg?

vba
  •  1
  • Berryl  · 技术社区  · 7 年前

    假设一个变量是空的=0,我想看看是否有人有一个技巧来区分这两者,如下面的虚拟函数

    Function doGetFmt(v As Variant) As String
        dim s as String
        If IsEmpty(v) Then s ="Empty"
        If v=0 s= "zero"
        doGetFmt = s
    End Function
    
    0 回复  |  直到 7 年前
        1
  •  0
  •   Tim Williams    7 年前

    Dim v As Variant
    
    Debug.Print IsEmpty(v) '>> #true#
    Debug.Print v = 0      '>> true
    
    v = 0
    
    Debug.Print IsEmpty(v) '>> #false#
    Debug.Print v = 0      '>> true
    

    IsEmpty() 如果返回false,那么您就更接近您想要的。