是否可以使用IsNumeric动态检查变量的函数?

时间:2015-12-10 13:57:41

标签: excel vba excel-vba

我的VBA代码看起来像这样:

user enters value for variable
isnumeric(this variable)

user enters value for another variable
isnumeric(this variable now)

user enters value for a third variable
isnumeric(this third variable)

有没有办法用以下内容替换它:

user enters variable
call CheckIfThisIsNumeric

go back to other function and repeat this process several times

所以我不必在我的程序中写一百万次IsNumeric?

如果这是非正式的,请道歉。

1 个答案:

答案 0 :(得分:0)

正如我所看到的,唯一的方法是将变量放入全局变量中。一件坏事有两个原因:

  1. 你有一条较短的线,但之前还有一条线。提供全局变量的那个
  2. Global variable are to avoid as long as possible
  3. 不过,你可以做一个

    Public myVariableforCheckingNumericity As Variant
    (.../...)
    myVariableforCheckingNumericity = ThisVariable
    If CheckIfThisIsNumeric Then......
    
    Function CheckIfThisIsNumeric () As Boolean
        CheckIfThisIsNumeric  = IsNumeric(myVariableforCheckingNumericity)
    End Function
    

    然而,正如评论者所说,整体利益是消极的。