Link to home
Start Free TrialLog in
Avatar of cpatte7372
cpatte7372Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Excel Run-time error '13'

Hello Experts,

Can someone please tell me why I keep on getting a Run-time error, Type mismatch when I open my spreadsheet with the following code. When click on debug it highlights the following:

 If Stocks(i) <> .Value Then




Private Sub Worksheet_Calculate()
Dim cel As Range
Dim Addr As Variant, Targ As Variant
Static Stocks(3) As Double      'Starts with element 0
Dim i As Long, n As Long
Addr = Array("AQ3", "AV3", "AP3", "AU3")    'Watch these cells for price changes
Targ = Array(85, 85, 20, 20)                'Look for prices above these threshhold values
n = UBound(Stocks)
For i = 0 To n
    With Range(Addr(i))
        If Stocks(i) <> .Value Then
            Stocks(i) = .Value
            If .Value > Targ(i) Then
                Open "C:\Users\User\Documents\ABC.txt" For Append As #1  'Change path & name to suit
                Write #1, .Address(False, False), .Value, Date, Format(Time, "hh:mm:ss.ss")
                Close 1
            End If
        End If
    End With
Next
End Sub

Open in new window



Cheers
Avatar of cpatte7372
cpatte7372
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

For the Experts willing to assist the attached spreadsheet my provide further assistance.

Cheers
EE-TypeMismatch.xlsm
It's because AU3 contains a #DIV/0! error and therefore is not numeric. You coulod add a check to your code as follows:
Private Sub Worksheet_Calculate()
Dim cel As Range
Dim Addr As Variant, Targ As Variant
Static Stocks(3) As Double      'Starts with element 0
Dim i As Long, n As Long
Addr = Array("AQ3", "AV3", "AP3", "AU3")    'Watch these cells for price changes
Targ = Array(85, 85, 20, 20)                'Look for prices above these threshhold values
n = UBound(Stocks)
For i = 0 To n
    With Range(Addr(i))
        If Not IsError(.Value) Then
            If Stocks(i) <> .Value Then
                Stocks(i) = .Value
                If .Value > Targ(i) Then
                    Open "C:\Users\User\Documents\ABC.txt" For Append As #1  'Change path & name to suit
                    Write #1, .Address(False, False), .Value, Date, Format(Time, "hh:mm:ss.ss")
                    Close 1
                End If
            End If
        End If
    End With
Next
End Sub

Open in new window


Actually you might also want to check that it is numeric, as a non-numeric value would also cause your code to fail.  You could add an IF statement inside the error check like:

If Isnumeric(.Value) Then  .....
Andrew,

You're code seemed to have done the trick cheers mate
ASKER CERTIFIED SOLUTION
Avatar of andrewssd3
andrewssd3
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi sorry for the delay in awarding points.

Cheers mate.