0

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
1
  • remove VAL and it will work, since A is a string (text), not a value(number) . Also you could do it without variable : msgbox "My name is " & InputBox("Enter your name", "NAME") Commented Jan 16, 2015 at 15:36

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for clearing the doubt. Can you suggest me what should be used to resolve the above issue?
I'm not sure what you're asking. There is no issue, other than what is most likely improper use of the Val function. If you are having trouble, it might be helpful to provide some examples of the input and desired output.
I want to fetch a string through input box or may be through any other way.
The input box returns a string without using the Val function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.