VBA: Run-time error 13: Type mismatch

Strogg

New Member
Joined
Dec 5, 2018
Messages
5
Hey everybody. Error 13 (type mismatch) occurs when I try to run this macro.
Nevertheless, I can't find out which data types are incompatible. Could you help me to find it out, please?

Here is the code.
Code:
Public Sub Macro1()
    Dim m, n, p As Double
    Dim x1, x2, xmin, z As Double
    m = InputBox("Input m", "Input", "")
    n = InputBox("Input n", "Input", "")
    p = InputBox("Input p", "Input", "")
    x1 = (m ^ 2 + n - Sqr(Abs(m * p))) / 10
    If m > 10 And m < 15 Then
    x2 = m + n
    Else
    x2 = p - m
    End If
    Dim usl As Boolean
    usl = x2 > x1
    If usl Then
        x1 = xmin
    Else
        x2 = xmin
    End If
    z = xmin + 15
    Dim answer As String
    answer = "x1 = " & "x2 = " & "xmin = " And "z = "
    MsgBox answer, "vbInforamtion", "Answer"
End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
The way you're dimming your variables isn't exactly correct. When you write a line like:

Dim m, n, p as Double

It's not dimming all of them as double, it's only dimming p as double. m and n are both being dimmed as variant here, which allows for non-numerical values.

Explicitly define each variable type:
Dim m as double, n as double, p as double
 
Upvote 0
Your Msgbox statement is wrong - it should be:

Code:
MsgBox answer, vbInformation, "Answer"
 
Upvote 0
Code:
Public Sub Macro1()
    Dim m As Double, n As Double, p As Double
    Dim x1 As Double, x2 As Double, xmin As Double, z As Double
    m = InputBox("Input m", "Input", "")
    n = InputBox("Input n", "Input", "")
    p = InputBox("Input p", "Input", "")
    x1 = (m ^ 2 + n - Sqr(Abs(m * p))) / 10
    If m > 10 And m < 15 Then
    x2 = m + n
    Else
    x2 = p - m
    End If
    Dim usl As Boolean
    usl = x2 > x1
    If usl Then
        x1 = xmin
    Else
        x2 = xmin
    End If
    z = xmin + 15
    Dim answer As String
    answer = "x1 = " & x1 & " x2 = " & x2 & " xmin = " & xmin & " z = " & z
    MsgBox answer, vbInformation, "Answer"
End Sub
Adjusted as suggested. Your answer provided no info and used "AND" instead of "&". HTH. Dave
 
Upvote 0

Forum statistics

Threads
1,223,247
Messages
6,171,007
Members
452,374
Latest member
keccles

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