Hi All,

I have created a program for calculating average of 4 numbers, however, Im getting an error as "Run Time Error 13 Type Mismatch" on the Click event of "Calculate Average" command button.

The code written for the Click Event of "Calculate Average" command button goes as :

Private Sub cmd_avg_Click()
Dim num1 As Integer
Dim num2 As Integer
Dim num3 As Integer
Dim num4 As Integer
Dim avg As Integer
Dim msg As String
avg = txt_avg.Text // Line causing the error message //
num1 = txt_num1.Text
num2 = txt_num2.Text
num3 = txt_num3.Text
num4 = txt_num4.Text
avg = (num1 + num2 + num3 + num4) / 4
msg = "The average of four given numbers is " + avg
txt_avg.Text = msg

End Sub

Any help will be greatly appreciated, thanks

You are trying to do math on a string value ...

avg = txt_avg.Text // Line causing the error message //
num1 = txt_num1.Text
num2 = txt_num2.Text
num3 = txt_num3.Text
num4 = txt_num4.Text

try something like

num1 = val(txt_num1.Text)

etc..

then avg = ( num1 + ... ) /4

Also keep in mind that the average of 4 integers is not necessarily an integer itself.

Hey, thanks a lot.

I gave it a try but it didnt help. Should i try something else? Any suggestions??

Hi,

Comment the line causing average, as it is not necessary.

Dim avg as Currency
As Average need not necessarily be asn Integer.

Regards
Veena

Hi,
Sorry it was
Comment The Line Causing Error (not Average)

Hi All,

I have created a program for calculating average of 4 numbers, however, Im getting an error as "Run Time Error 13 Type Mismatch" on the Click event of "Calculate Average" command button.

The code written for the Click Event of "Calculate Average" command button goes as :

Private Sub cmd_avg_Click()
Dim num1 As Integer
Dim num2 As Integer
Dim num3 As Integer
Dim num4 As Integer
Dim avg As Integer
Dim msg As String
avg = txt_avg.Text // Line causing the error message //
num1 = txt_num1.Text
num2 = txt_num2.Text
num3 = txt_num3.Text
num4 = txt_num4.Text
avg = (num1 + num2 + num3 + num4) / 4
msg = "The average of four given numbers is " + avg
txt_avg.Text = msg

End Sub

Any help will be greatly appreciated, thanks

try this code;
avg = format((val(txt_num1.text) + val(txt_num2.text) + val(txt_num3.text)) /4, "##")
msg = "The average of four given numbers is " & avg

Since you declared the variables as integers, you could convert the string values to integer by using cInt function. try like this

num1 = cint(txt1.text)
num2 = cint(txt2.text)
num3 = cint(txt3.text)
num4 = cint(txt4.text)

avg = cint(cint(num1 + num2 + num3 + num4) / 4)

this should work without any error.

- bls

Since you declared the variables as integers, you could convert the string values to integer by using cInt function. try like this

num1 = cint(txt1.text)
num2 = cint(txt2.text)
num3 = cint(txt3.text)
num4 = cint(txt4.text)

avg = cint(cint(num1 + num2 + num3 + num4) / 4)

this should work without any error.

- bls

Convert all the numbert to integer, then add them and convert that number to integer, then divide by 4 and convert that to an integer. Isn't that a little overkill?

try writing
Dim num1 As double
Dim num2 As double
Dim num3 As double
Dim num4 As double
Dim avg As double
Dim msg As String

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.