cpatte7372
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
Cheers
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
Cheers
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
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 .....
If Isnumeric(.Value) Then .....
ASKER
Andrew,
You're code seemed to have done the trick cheers mate
You're code seemed to have done the trick cheers mate
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi sorry for the delay in awarding points.
Cheers mate.
Cheers mate.
ASKER
Cheers
EE-TypeMismatch.xlsm