saurabh
saurabh

Reputation: 1

Excel VBScript: String issue

After entering any characters, the value which is stored in variable A is "0". Can anyone please assist me where I am going wrong as it is working fine if I enter number

Rookie

Public Sub MyFirstProgram()

 Dim A As String
 A = Val(InputBox("Enter your name", "NAME"))
 MsgBox "My name is " & A

End Sub

Upvotes: 0

Views: 39

Answers (1)

David Zemens
David Zemens

Reputation: 53663

The Val function converts a string to a Double numeric type.

Presumably, the names you are entering cannot be converted to a valid number, so the result is 0.

http://office.microsoft.com/en-us/excel-help/HV080557263.aspx

The Val function stops reading the string at the first character it can't recognize as part of a number.

So if you do something like =Val("123steve") it will return the numeric component: 123, but if you do =Val("Ebeneezer Scrooge") it stops, per the above remark -- since no characters have been converted to numeric value, it returns 0.

Upvotes: 2

Related Questions