Solved

VBA Run-Time Error 13

Posted on 2011-02-11
10
321 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:shoaibs
Comment Utility
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
Comment Utility
Did you try Rory's code?

Sid
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:shoaibs
Comment Utility
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
Comment Utility
>>"my next question is ..."
And hence, should be your next question... :)
0
 
LVL 85

Expert Comment

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

Author Comment

by:shoaibs
Comment Utility
Yes, they are used as CF formulas
0
 
LVL 85

Expert Comment

by:Rory Archibald
Comment Utility
So which cells are you actually wanting to test?
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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 article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

743 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

13 Experts available now in Live!

Get 1:1 Help Now