这不是您使用的 Windows 版本的新问题。它始终基于机器的区域设置。
我为我的应用程序所做的是创建了自己的函数:
Public Function IsNumber(ByRef Expression As Variant) As Boolean
Select Case VarType(Expression)
Case vbInteger, vbLong, vbSingle, vbDouble, vbCurrency, vbDate, vbBoolean, vbDecimal, vbByte
IsNumber = True
Case vbString
Dim Negative As Boolean
Dim Number As Boolean
Dim Period As Boolean
Dim Positive As Boolean
Dim X As Long
For X = 1& To Len(Expression)
Select Case Mid$(Expression, X, 1&)
Case "0" To "9"
Number = True
Case "-"
If Period Or Number Or Negative Or Positive Then Exit Function
Negative = True
Case "."
If Period Or Exponent Then Exit Function
Period = True
Case "E", "e"
If Not Number Then Exit Function
If Exponent Then Exit Function
Exponent = True
Number = False
Negative = False
Period = False
Case "+"
If Not Exponent Then Exit Function
If Number Or Negative Or Positive Then Exit Function
Positive = True
Case vbSpace, vbTab, vbVerticalTab, vbCr, vbLf, vbFormFeed
If Period Or Number Or Exponent Or Negative Then Exit Function
Case Else
Exit Function
End Select
Next X
IsNumber = Number
End Select
End Function
如果您明确地处理字符串,那么您可以简化该功能。而且您可能不想处理指数,所以这可能更适合:
Public Function IsNumber(ByRef Expression As String) As Boolean
Dim Negative As Boolean
Dim Number As Boolean
Dim Period As Boolean
Dim X As Long
For X = 1& To Len(Expression)
Select Case Mid$(Expression, X, 1&)
Case "0" To "9"
Number = True
Case "-"
If Period Or Number Or Negative Then Exit Function
Negative = True
Case "."
If Period Then Exit Function
Period = True
Case vbSpace, vbTab, vbVerticalTab, vbCr, vbLf, vbFormFeed
If Period Or Number Or Negative Then Exit Function
Case Else
Exit Function
End Select
Next X
IsNumber = Number
End Function
当您需要转换数字时,请使用Str()
而不是CLng()
/ CInt()
/ CDbl()
/ CSng()
/ Val()
。 Str()
无论语言环境如何,都将句点视为小数,就像我IsNumber()
上面的函数一样。