run-time error 13: type mismatch in VBA (Excel 2007)

zeno

Board Regular
Joined
Feb 16, 2012
Messages
71
Hello,
I'm writing a VBA program and receive "run-time error 13: type mismatch"

In the code below the line in bold receives the error.

Rich (BB code):
<code>   Let FactornEMA(nEMA) = 2 / (FactorMA(nEMA) + 1)   'run-time error 13: type mismatch
  Let Cells(1 + nData, colEMA(nEMA)) = FactornEMA(nEMA) * (Cells(1 + nData, 4) - Cells(nData, 4))                                </code><code>  </code><code>Else       
  End If</code>
For clarification, FactorMA(nEMA) is an array of about 2 possible values a and b.

I have all parts of the line defined as variables and I use that calculation as a constant in the next line.
Can somebody tell me why I receive this error?
Thanks.
 
Last edited:

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
zeno,

FactorMA(nEMA) is an array of about 2 possible values a and b


If FactorMA(nEMA) can only be "a" or "b", then you cannot use them to perform mathematical operations, such as addition and division.

This will give a Runtime error as you've described. Here's an example that will also result in the type mismatch. This happens because you cannot add to a non-numeric value:
Code:
Sub tgr()
    
    MsgBox "a" + 1
    
End Sub
 
Upvote 0
Thank you for your response.
What is your suggestion to solve this?
I have to make this calculation. So I can calculate this constant first and then use it in the next line for another calculation.
Or I can calculate that constant for every row and so make one single, bigger calculation?
 
Upvote 0
What, exactly, is the calculation you are trying to make with letters? You need to use numbers to perform calculations...

2/(a+1) won't work, but 2/(1+1) will.
 
Upvote 0
I put more code to provide better context.
the constant 'FactorMA(nEMA)' can only have 2 values. I thought that calculating that constant first might make the calculation in the next line easier. Maybe it's better to only make 1 single calculation instead of 2 separate ones?

Code:
<code>Dim FastPeriod, SlowPeriod, MACDPeriod, FactorMA, FactornEMA   Dim nEMA Dim colEMA  For nEMA = 1 To 2      Cells(1, colEMA(nEMA)) = "EMA-" & FactorMA(nEMA)     Cells(1, colEMA(3)) = "EMA" & FactorMA(2) & "-EMA" & FactorMA(1)      For nData = 1 To Data         DataCount = DataCount + 1                        If DataCount >= FactorMA(nEMA) Then             If DataCount > FactorMA(nEMA) Then                                               Else             End If                        Let FactornEMA(nEMA) = 2 / (FactorMA(nEMA) + 1)   'run-time error 13: type mismatch                        Let Cells(1 + nData, colEMA(nEMA)) = FactornEMA(nEMA) * (Cells(1 + nData, 4) - Cells(nData, 4))                        Else         End If     Next nData

The view of this code displays in only 1 line. I have not written it that way though.
</code>
 
Upvote 0

Forum statistics

Threads
1,214,967
Messages
6,122,503
Members
449,090
Latest member
RandomExceller01

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