Convert text to number

G

Guest

I am using an input box to assign a number to a variable. e.g
EC = InputBox("Enter the number of the Position Description from the list
below." )

However, I think if the input is say 13, the variable stores this as a text
"13".
1. Am I correct in assuming this?
2. Can I convert this from text to a number with code?

Any assistance would be appreciated.
 
G

Gord Dibben

The number entered should be a number.

Sub qwer()
Dim asdf as Long
asdf = InputBox("enter a number")
Range("H1").Value = asdf
End Sub

H1 is number 13 after running the above and entering 13 in the inputbox.


Gord Dibben MS Excel MVP
 
G

Guest

Yes, the inputbox function returns text. If EC is declared as a numeric
type, vba would coerce the input to a number by itself.

Dim EC As Long
EC = Inputbox(....)

Or, you can explicitly convert the input
EC = Clng(Inputbox(..))

However, you would get an error if text was input that could not be
converted to numeric unless you tested it using IsNumeric first
Dim EC As Variant
EC = Inputbox(...)
If IsNumeric(EC) Then EC = Clng(EC)

Or use the Inputbox method, which allows you to specify the type of data
that can be input.
EC = Application.Inputbox(Prompt:="Input", Type:=1)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top