Reputation: 39
i am working on new excel vba project for work.My question is how to sum up values inside of a textbox.For example;if i wrote "50+50" in textbox1,upon exit i want textbox1 to show value of "100".I tried several methots but none of them seem to work.Thanks anyone in advance to answer.
Upvotes: 0
Views: 812
Reputation: 324
Try this:
Sub CalcSum()
Dim a as Variant
Dim b as Variant
a = inputbox("Please enter the First Value")
If a ="" then
Msgbox("Nothing was entered for 1st value")
Exit Sub
End If
b = inputbox("Please enter the Second Value")
If b ="" then
Msgbox("Nothing was entered for 2nd value")
Exit Sub
End If
Msgbox(a & " + " & b & " is: " & a+b)
End Sub
Alternatively if you are using a userform, you could use something like the below after the above code without the msgbox line:
textbox1.value=a+b
Upvotes: 0
Reputation: 152450
As per my comment look into the Application.Evaluate() function:
Sub evall()
Dim t As String
Dim h As Double
t = InputBox("Formula")
h = Application.Evaluate(t)
MsgBox h
End Sub
Upvotes: 2