Need Help With Error In VBA Code

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
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hey Juicyblunts...when you post code please put it between the code tags (click the # icon). This makes the code much more readable.

I noticed one error you made in a couple of spots:

Namely, this:
Code:
MsgBox "The Answer Will Be:" + vbCrLf + Number_1 + vbCrLf + "Write This Down!"

should be this:
Code:
MsgBox "The Answer Will Be:" & vbCrLf & Number_1 & vbCrLf & "Write This Down!"

Also, I like to place all my variable declarations in one place at the beginning of my programs, rather than scattering them throughout as you have done, so that they are easier to reference.

Regards,

CJ
 
Last edited:
Upvote 0
Hey Juicyblunts...when you post code please put it between the code tags (click the # icon). This makes the code much more readable.

I noticed one error you made in a couple of spots:

Namely, this:
Code:
MsgBox "The Answer Will Be:" + vbCrLf + Number_1 + vbCrLf + "Write This Down!"

should be this:
Code:
MsgBox "The Answer Will Be:" & vbCrLf & Number_1 & vbCrLf & "Write This Down!"

Also, I like to place all my variable declarations in one place at the beginning of my programs, rather than scattering them throughout as you have done, so that they are easier to reference.

Regards,

CJ

Thanks for your response. I will remember next time to place code in the format you mentioned. I appreciate your input, however these are personal preferences more than anything as none of that affects the output. Were you able to tell from the code why I am getting an error when I run this program? After entering the first 3 digit number I get an error message "error-13" or something I'm not at my computer at the moment but from what I could find online it seems to be related to the input not being the correct variable type. I tried changing it to integer and it still does not work.
 
Upvote 0
Using & instead of + is not a personal preference, and it's the use of + that's causing the 'type mismatch' error you describe.

Also, this is incorrect syntax,
Code:
MsgBox InputPrompt, vbCrLf + Number_5
the 2nd argument of MsgBox is for the buttons (OK/Cancel/Yes/No etc) you want to appear.


Try this.
Code:
Sub Mathemagic()

Dim InputPrompt As String
Dim InputQuestion As String
Dim Number_0 As Variant
Dim Number_1 As Variant
Dim Number_2 As Variant
Dim Number_3 As Variant
Dim Number_4 As Variant
Dim Number_5 As Variant
Dim Number_6 As Variant

    InputQuestion = "Please Enter a 3 Digit Number."

    InputPrompt = "Now Add:"

    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

        Number_1 = Number_0 + 1998

        MsgBox "The Answer Will Be:" & vbCrLf & Number_1 & vbCrLf & "Write This Down!"

    End If

    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

        Number_3 = 999 - Number_2

        MsgBox InputPrompt & vbCrLf & Number_3

    End If

    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

        Number_5 = 999 - Number_4

        MsgBox InputPrompt & vbCrLf & Number_5

    End If

    Number_6 = Number_0 + Number_2 + Number_3 + Number_4 + Number_5

    UserForm4.Show

End Sub
 
Upvote 0
Using & instead of + is not a personal preference, and it's the use of + that's causing the 'type mismatch' error you describe.

Also, this is incorrect syntax,
Code:
MsgBox InputPrompt, vbCrLf + Number_5
the 2nd argument of MsgBox is for the buttons (OK/Cancel/Yes/No etc) you want to appear.


Try this.
Code:
Sub Mathemagic()

Dim InputPrompt As String
Dim InputQuestion As String
Dim Number_0 As Variant
Dim Number_1 As Variant
Dim Number_2 As Variant
Dim Number_3 As Variant
Dim Number_4 As Variant
Dim Number_5 As Variant
Dim Number_6 As Variant

    InputQuestion = "Please Enter a 3 Digit Number."

    InputPrompt = "Now Add:"

    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

        Number_1 = Number_0 + 1998

        MsgBox "The Answer Will Be:" & vbCrLf & Number_1 & vbCrLf & "Write This Down!"

    End If

    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

        Number_3 = 999 - Number_2

        MsgBox InputPrompt & vbCrLf & Number_3

    End If

    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

        Number_5 = 999 - Number_4

        MsgBox InputPrompt & vbCrLf & Number_5

    End If

    Number_6 = Number_0 + Number_2 + Number_3 + Number_4 + Number_5

    UserForm4.Show

End Sub

Hmm, I assumed it was because I had changed the "+" to "&" as suggested and it made no difference, so this can't be the problem since it was still bringing up the error message after changing that. And yes, I did catch that syntax error while messing around with it last night so that's been corrected as well. I will try to copy paste the code you gave me when I get home and let you know how it works out, thank you!!!
 
Upvote 0
After making the corrections that Norrie and I highlighted above, your code seemed to work fine for me. If you are still getting error messages please let us know which line is being flagged. Also, if you have another macro calling this one, post that as the trouble may begin there.

Regards,

CJ
 
Upvote 0

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,077
Latest member
Jocksteriom

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