Solved

Excel Run-time error '13'

Posted on 2011-09-02
7
164 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
Comment Utility
For the Experts willing to assist the attached spreadsheet my provide further assistance.

Cheers
EE-TypeMismatch.xlsm
0
 
LVL 17

Expert Comment

by:andrewssd3
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:cpatte7372
Comment Utility
Andrew,

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

Accepted Solution

by:
andrewssd3 earned 500 total points
Comment Utility
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
Comment Utility
Hi sorry for the delay in awarding points.

Cheers mate.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Index & Match Help 2 22
Using EXCEL to count the number of Rows 4 44
Copy the formula in excel 8 26
Gantt chart 2 9
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

772 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

14 Experts available now in Live!

Get 1:1 Help Now