A. Jones
A. Jones

Reputation: 39

InputBox confusing Decimal entries with strings

I am trying to use an InputBox function to be able to have a user input data. However, the data mainly consists of decimal values. When I am testing my program and I go to subtract two values, both entered from InputBox, it does not perform a mathematical operation.

thckmax = InputBox("What is the maximum nominal thickness?", "Enter Max Nominal Thickness Measurement")
thckmin = InputBox("What is the minimum nominal thickness?", "Enter Min Nominal Thickness Measurement")
thcknom = (thckmax + thckmin)

If I were to enter .05 and .04 respectively, it displays the preceding code as:

thcknom is .05.04

I am trying to perform the mathematical operation, addition, to the two variables; however, it is just "adding" the two strings side-by-side. How do I fix this issue?

Upvotes: 2

Views: 74

Answers (2)

iDevlop
iDevlop

Reputation: 25252

InputBox allows you to define the input type. Since you did not specify one, the default is used, which is TEXT.
Something like

thckmax = Application.InputBox(Prompt:="What is the maximum nominal thickness?", Type:=1)

guarantees you get a number, eventually preventing decimal symbol confusion.

Upvotes: 1

BruceWayne
BruceWayne

Reputation: 23283

You need to declare the variables as either Double or Single, depending on the expected input:

Sub t()
Dim thckmax As Double, thckmin As Double, thcknom As Double
thckmax = InputBox("What is the maximum nominal thickness?", "Enter Max Nominal Thickness Measurement")
thckmin = InputBox("What is the minimum nominal thickness?", "Enter Min Nominal Thickness Measurement")
thcknom = (thckmax + thckmin)
Debug.Print thcknom
End Sub

See VBA's data types for more info.

Also, I suggest adding Option Explicit to the very top of your modules, which forces you to declare all variables.

Upvotes: 2

Related Questions