Solved

Excel Run-time error '13'

Posted on 2011-09-02
7
165 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:cpatte7372
  • 3
  • 3
7 Comments
 

Author Comment

by:cpatte7372
ID: 36472608
For the Experts willing to assist the attached spreadsheet my provide further assistance.

Cheers
EE-TypeMismatch.xlsm
0
 
LVL 17

Expert Comment

by:andrewssd3
ID: 36472762
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


0
 
LVL 17

Expert Comment

by:andrewssd3
ID: 36472769
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  .....
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:cpatte7372
ID: 36472841
Andrew,

You're code seemed to have done the trick cheers mate
0
 
LVL 17

Accepted Solution

by:
andrewssd3 earned 500 total points
ID: 36472862
Good - happy to help... but I'd still like the points (and for the conditional formatting one I answered for you earlier)  ;-)
0
 

Author Closing Comment

by:cpatte7372
ID: 36519710
Hi sorry for the delay in awarding points.

Cheers mate.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now