Verifying user input

warpedone

Board Regular
Joined
May 1, 2002
Messages
139
I'm an idiot and can't figure out what's wrong with this. I want to ask for 3 angles of a triangle and make sure they total 180 degrees and if not, ask again.

what's wrong with this? I get caught in an infinite loop

Do Until j = 180
angleABC = InputBox("Enter the Angle ABC (in degrees)")
angleBCA = InputBox("Enter the Angle BCA (in degrees)")
angleCAB = InputBox("Enter the Angle CAB (in degrees)")
j = angleABC + angleBCA + angleCAB
Loop
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
If you dimension the variables as Double (or Integer if you're only looking at whole numbers), it'll work. Since you didn't declare them at all, they're strings, and the + was concatenating the strings. Hope that helps!
 
Upvote 0
The only problem with dimensioning the variables as a number format is if the user hits Cancel, it will generate a Type Mismatch error.

Perhaps:

Code:
Sub test()
Dim angleabc, angleBCA, angleCAB
Do Until j = 180
angleabc = InputBox("Enter the Angle ABC (in degrees)")
angleBCA = InputBox("Enter the Angle BCA (in degrees)")
angleCAB = InputBox("Enter the Angle CAB (in degrees)")
j = Val(angleabc) + Val(angleBCA) + Val(angleCAB)
If j <> 180 Then MsgBox "Total of all angles must equal 180"
Loop
End Sub
 
Upvote 0

Forum statistics

Threads
1,226,504
Messages
6,191,425
Members
453,657
Latest member
DukeJester

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top