error 13

acaccamise

New Member
Joined
Apr 25, 2016
Messages
4
I am doing a code and i keep getting "run-time error 13" on the line
MaxChars = InputBox("Maximum number of characters:")
Someone please help!

The code:
Public Sub AestheticAgeCalc()

Dim MaxChars As Integer
Dim CharCount As Integer
Dim CharName As String
Dim ActualAge As Double
Dim AestheticAge As Double
Dim TwentyFirstYear As Double
Dim DeathYear As Integer
Dim OldYear As Integer
Dim CurrentYear As Integer
Dim OldAestheticAge As Double
Dim GrowthRate As Double


MaxChars = InputBox("Maximum number of characters:")

CharCount = 1


Do While CharCount <= MaxChars
CharName = InputBox("Character name:")
If CharName = "" Then
Exit Sub
End If
ActualAge = InputBox("How old is " & CharName & "?")
If ActualAge <= 21 Then
AestheticAge = ActualAge
MsgBox "" & CharName & " looks " & AestheticAge & " year(s) old."
CharCount = CharCount + 1
ElseIf ActualAge > 21 Then
CurrentYear = InputBox("Year during which " & CharName & " is " & ActualAge & ":")
TwentyFirstYear = InputBox("" & CharName & "'s 21st birth year:")
If TwentyFirstYear > CurrentYear Then
MsgBox "This contradicts previously inputted data!"
Exit Sub
End If
DeathYear = InputBox("" & CharName & "'s death year:")
OldYear = InputBox("Enter an old year (no earlier than " & CharName & "'s 21st birth year) for which you know " & CharName & "'s corresponding age:")
If OldYear < TwentyFirstYear Then
MsgBox "This year is too early!"
Exit Sub
ElseIf OldYear > CurrentYear Then
MsgBox "This year is too late!"
Exit Sub
End If
OldAestheticAge = InputBox("How old does " & CharName & " look in " & OldYear & "?")
GrowthRate = InputBox("How many years does it take " & CharName & " to appear to age one year?")
AestheticAge = ((CurrentYear - OldYear) / GrowthRate) + OldAestheticAge
MsgBox "" & CharName & " looks " & AestheticAge & " years old."
CharCount = CharCount + 1
End If
Loop

End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I don't get that error when I run your code. Technically, the Inputbox function returns a string and you have dimensioned MaxChars as type Integer so there is a mismatch (I think Error 13 is a type mismatch error), but VBA is very good at recognizing numbers masquerading as strings, and as I said, I don't get that error (or any error).

You might try dimensioning MaxChars as type Variant to see if that circumvents the error.
 
Upvote 0
I don't get that error when I run your code. Technically, the Inputbox function returns a string and you have dimensioned MaxChars as type Integer so there is a mismatch (I think Error 13 is a type mismatch error), but VBA is very good at recognizing numbers masquerading as strings, and as I said, I don't get that error (or any error).

You might try dimensioning MaxChars as type Variant to see if that circumvents the error.
it pops up when you click cancel on the first input box
 
Upvote 0
it pops up when you click cancel on the first input box
Well you should have said that your original post. Dimension MaxChars as Variant and add this line after the inputbox line:

If MaxChars = "" Then Exit Sub

that will handle the user clicking Cancel on the inputbox.
 
Upvote 0
Well you should have said that your original post. Dimension MaxChars as Variant and add this line after the inputbox line:

If MaxChars = "" Then Exit Sub

that will handle the user clicking Cancel on the inputbox.
Thank you so much! i'm sorry, just forgot to add it. Thanks again!
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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