Solved

VBA Run-Time Error 13

Posted on 2011-02-11
10
322 Views
Last Modified: 2012-06-21
Hi all,

When I input the following code, I get runtime error 13, type mismatch upon running it. This only occurs when the referenced cells (F8,H8,G8,I8) are blank. If I do input numbers into those cells, the code runs fine. Any ideas why this may be? Thanks.

Private Sub worksheet_deactivate()

If Evaluate("IF(AND(AND(OR(((F8-I8)/I8>0.2),((F8-I8)/I8<-0.2)),NOT(ISBLANK(I8)),NOT(ISBLANK(F8))),AND(NOT(ISBLANK(F8)),NOT(ISBLANK(G8)),NOT(ISBLANK(H8)),NOT(SUM(G8+H8)=F8))),TRUE,FALSE)") Then
MsgBox "Test", vbOKCancel

End If
End Sub
0
Comment
Question by:shoaibs
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 34871660
Try this

Private Sub worksheet_deactivate()
    On Error Resume Next
    If Evaluate("IF(AND(AND(OR(((F8-I8)/I8>0.2),((F8-I8)/I8<-0.2)),NOT(ISBLANK(I8)),NOT(ISBLANK(F8))),AND(NOT(ISBLANK(F8)),NOT(ISBLANK(G8)),NOT(ISBLANK(H8)),NOT(SUM(G8+H8)=F8))),TRUE,FALSE)") Then
    MsgBox "Test", vbOKCancel
    
    End If
    On Error GoTo 0
End Sub

Open in new window


Sid
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 34871715
Or assign it to a Variant and use IsError:
Private Sub worksheet_deactivate()
Dim varReturn
varReturn = Evaluate("IF(AND(AND(OR(((F8-I8)/I8>0.2),((F8-I8)/I8<-0.2)),NOT(ISBLANK(I8)),NOT(ISBLANK(F8))),AND(NOT(ISBLANK(F8)),NOT(ISBLANK(G8)),NOT(ISBLANK(H8)),NOT(SUM(G8+H8)=F8))),TRUE,FALSE)") 
If Not IsError(varreturn) Then
  If varReturn Then MsgBox "Test", vbOKCancel
End If
End Sub

Open in new window

0
 
LVL 6

Expert Comment

by:Eric Zwiekhorst
ID: 34871747
Dear shoaibs,


the problem is caused because the formula has this F8-I8)/I8>0.2),((F8-I8)/I8<-0.2)),
and if the cells or blank excel can not calulate..

You have to test before if the cells are empty and if not you can calculate the rest..

Kind regards

Eric
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

Author Comment

by:shoaibs
ID: 34871825
Hi Sid,

Thanks for the response. I'm using the popup message as a validation of sorts. When I use your code, the textbox pops up even if the cells are blank, I need the msgbox to only appear if the evaluate statement is true. Any ideas on how to accomplish this?

Thanks.
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 34871985
Did you try Rory's code?

Sid
0
 

Author Comment

by:shoaibs
ID: 34872030
Rory's code works great, my next question is how would I go about adding additional evaluates to that e.g. the same formula but for the row below it. I want one single msgbox to popup if any of the rows (I have about 10) make the formula true.

Thanks.
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34872072
>>"my next question is ..."
And hence, should be your next question... :)
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34872160
BTW, are these formulas currently in use as CF formulas for the cells you want to test?
0
 

Author Comment

by:shoaibs
ID: 34872232
Yes, they are used as CF formulas
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34872239
So which cells are you actually wanting to test?
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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