如何在定义的列表之外查找字符 (excel 2016)



我对Excel中的VBA宏非常陌生,我正在尝试查找/编写一个新宏来执行以下任务:从特定单元格中获取一个字符串,并检查它是否包含我定义的列表中的任何无效字符。例如:有效字符包括:任何数字、pi、e、log、ln、+、-等...无效字符是:不在上述列表中的任何内容(即_或%(如果字符串为 3+x,则不会引发错误。 如果x_2字符串,则会引发错误并停止任何进一步的计算。我一直没有成功找到这个确切问题的解决方案,任何帮助将不胜感激。 谢谢!

我会从下面的函数开始。

'********************************************************************
'*                                                                  *
'*  Public Function InvalidInput                                    *
'*    Parameters:                                                   *
'*      InputString as String                                       *
'*      InvalidValue being searched for                             *
'*    Commentary:                                                   *
'*      If an invalid character is found in the input string then   *
'*      function returns True otherwise by default it returns False *
'*                                                                  *
Public Function InvalidInput(ByVal InputString As String, ByVal InvalidValue As String) As Boolean
    'Establish the default return value of the function
    Dim retVal As Boolean: retVal = False
    'if an invalid charater is found then set return value to True
    If InStr(InputString, InvalidValue) <> 0 Then retVal = True
    InvalidInput = retVal
End Function

最新更新