juicyblunts
New Member
- Joined
- Feb 8, 2017
- Messages
- 3
I have built a program simply for practice in VBA. The program is very simple and I have made a couple working versions of it, however when I try to include a loop to force a valid user input I continually run into problems. I have tested the loop block by itself and it works fine, but when combined with the rest of my code it hits a wall and I get an Error message. I am wondering if someone could look over this code and let me know where I have gone Wrong. I am thinking it may be with the variable type and I will keep playing around with it, but I have been at this for several hours now and any help would be greatly appreciated!!! The program is kind of pointless for a computer program, but it was really just to exercise what I have learned with VBA so far. It takes a 3 digit input from the user and generates an output. The output is the answer to an addition problem, specifically the sum of the 3 digit input provided by the user as well as 2 additional 3 digit numbers provided by the user and two 3 digit numbers generated from those inputs. Essentially, it answers the problem without knowing all of the numbers that will be added together. It is based on a mental math trick and when you look at the code you will probably figure out how it works pretty easily.
Here is My main subroutine:
Sub Mathemagic()
Dim InputQuestion As String
InputQuestion = "Please Enter a 3 Digit Number."
Dim InputPrompt As String
InputPrompt = "Now Add:"
Dim Number_0 As Variant
Do
Number_0 = Application.InputBox(InputQuestion, "Your number", Type:=1)
If TypeName(Number_0) = "Boolean" Then Exit Sub
Loop While Number_0 < 100 Or Number_0 > 999
If Number_0 > 99 Or Number_0 < 1000 Then
Dim Number_1 As Variant
Number_1 = Number_0 + 1998
MsgBox "The Answer Will Be:" + vbCrLf + Number_1 + vbCrLf + "Write This Down!"
End If
Dim Number_2 As Variant
Do
Number_2 = Application.InputBox(InputQuestion, "Your number", Type:=1)
If TypeName(Number_2) = "Boolean" Then Exit Sub
Loop While Number_2 < 100 Or Number_2 > 999
If Number_2 > 99 Or Number_2 < 1000 Then
Dim Number_3 As Variant
Number_3 = 999 - Number_2
MsgBox InputPrompt, vbCrLf + Number_3
End If
Dim Number_4 As Variant
Do
Number_4 = Application.InputBox(InputQuestion, "Your number", Type:=1)
If TypeName(Number_4) = "Boolean" Then Exit Sub
Loop While Number_4 < 100 Or Number_4 > 999
If Number_4 > 99 Or Number_4 < 1000 Then
Dim Number_5 As Variant
Number_5 = 999 - Number_4
MsgBox InputPrompt, vbCrLf + Number_5
End If
Dim Number_6 As Variant
Number_6 = Number_0 + Number_2 + Number_3 + Number_4 + Number_5
UserForm4.Show
End Sub
Here is My main subroutine:
Sub Mathemagic()
Dim InputQuestion As String
InputQuestion = "Please Enter a 3 Digit Number."
Dim InputPrompt As String
InputPrompt = "Now Add:"
Dim Number_0 As Variant
Do
Number_0 = Application.InputBox(InputQuestion, "Your number", Type:=1)
If TypeName(Number_0) = "Boolean" Then Exit Sub
Loop While Number_0 < 100 Or Number_0 > 999
If Number_0 > 99 Or Number_0 < 1000 Then
Dim Number_1 As Variant
Number_1 = Number_0 + 1998
MsgBox "The Answer Will Be:" + vbCrLf + Number_1 + vbCrLf + "Write This Down!"
End If
Dim Number_2 As Variant
Do
Number_2 = Application.InputBox(InputQuestion, "Your number", Type:=1)
If TypeName(Number_2) = "Boolean" Then Exit Sub
Loop While Number_2 < 100 Or Number_2 > 999
If Number_2 > 99 Or Number_2 < 1000 Then
Dim Number_3 As Variant
Number_3 = 999 - Number_2
MsgBox InputPrompt, vbCrLf + Number_3
End If
Dim Number_4 As Variant
Do
Number_4 = Application.InputBox(InputQuestion, "Your number", Type:=1)
If TypeName(Number_4) = "Boolean" Then Exit Sub
Loop While Number_4 < 100 Or Number_4 > 999
If Number_4 > 99 Or Number_4 < 1000 Then
Dim Number_5 As Variant
Number_5 = 999 - Number_4
MsgBox InputPrompt, vbCrLf + Number_5
End If
Dim Number_6 As Variant
Number_6 = Number_0 + Number_2 + Number_3 + Number_4 + Number_5
UserForm4.Show
End Sub