VBA Val 函数返回字符串中包含的数字,直到找到非数字字符。
该函数只接受“.”作为小数点分隔符。
Val(文本)
使用 Val 函数将各种字符的字符串作为数字返回:
Sub ValExample()
MsgBox Val("1") '返回:1
MsgBox Val(" 1 ") '返回:1
MsgBox Val(1) '返回:1
MsgBox Val("1h") '返回:1
MsgBox Val("h1") '返回:0
MsgBox Val("1 number") '返回:1
MsgBox Val("number 1") '返回:0
MsgBox Val("2 2") '返回:22
MsgBox Val("2.2") '返回:2.2
MsgBox Val("2,2") '返回:2
MsgBox Val("75000 Kyiv") '返回:75000
MsgBox Val("Kyiv 75000") '返回:0
MsgBox Val("Excel") '返回:0
End Sub