Prompted by a comment from Sam on Match vs Find I thought I would take a look at Excel’s rather quirky Evaluate method with Excel 2010 to see how it performed.
The Evaluate method internally uses Excel’s formula parser and calculator, and this makes it surprisingly powerful. You can use it on virtually any kind of formula, range reference or Defined Name. But, as we will see, it does have a number of strange “quirks” that you have to navigate around.
Depending on the context Evaluate will either return an object (for example a Range) or values.
I will be using exactly the same test setup of 100000 rows of randomly generated XY pairs and timing routine as in Match vs Find, so you can directly compare the results.
Using the square brackets [ ] shortcut for Evaluate
Sam’s comment suggested using [COUNTIFS] to see how the timing compared with MATCH and FIND. Of course its not quite the same thing because the loop on Match and Find allows the VBA to do something for each XY pair found. Sam’s VBA looks like this:
Sub FindXY_COUNTIFS1() Dim j As Long Dim dTime As Double dTime = Microtimer j = [COUNTIFS(A1:A100000,"x",B1:B100000,"y")] Debug.Print "COUNTIFS1 " & j & " " & (Microtimer - dTime) * 1000 End Sub
It takes 11.6 millisecs to find the 25135 XY pairs generated using a constant of 0.5 in the test data generator.
[ ] is a shortcut for Application.Evaluate. The advantage of using the [ ] brackets is that it is concise and the formula inside the [ ] is exactly the same as when used in a worksheet cell. The disadvantage is that you cannot generate the formula as a string. I tend to only use this notation when evaluating my hidden workbook-scoped defined names, because they are not likely to change. (Of course sometimes I get lazy …)
Using Application.Evaluate instead of [ ]
You can use Evaluate or Application.Evaluate with a string instead of the [ ]
j = Evaluate("COUNTIFS(A1:A100000," & Chr(34) & "x" & Chr(34) & ",B1:B100000," & Chr(34) & "y" & Chr(34) & ")")
The timing is virtually identical to the [ ] shortcut method.
Application.Evaluate and the Activesheet
One trap for the unwary with [ ] , Evaluate and Application.Evaluate is that all references that do not contain a worksheet (unqualified references like A1:A100000) are assumed to refer to whatever the Active sheet currently happens to be.
So if you are going to use Application.Evaluate you should always use a qualified reference (Sheet1!A1:A100000) unless you like your code to live dangerously.
Worksheets and Charts also have an Evaluate Method. When you use these methods unqualified references are assumed to refer to the worksheet or chart.
Sub FindXY_COUNTIFS3() Dim j As Long Dim dTime As Double dTime = Microtimer j = Worksheets("Sheet1").Evaluate("COUNTIFS(A1:A100000," & Chr(34) & "x" & Chr(34) & ",B1:B100000," & Chr(34) & "y" & Chr(34) & ")") Debug.Print "COUNTIFS3 " & j & " " & (Microtimer - dTime) * 1000 End Sub
Now for the surprise: Worksheet.Evaluate is twice as fast as Application.Evaluate!
(actually 6.1 millisecs as opposed to 11.6 millisecs)
I am fairly sure that there is a bug in Application.Evaluate that actually does the evaluation twice.
Certainly if you use Application.evaluate on a UDF the UDF will be executed twice.
In the real world I have never actually used Chart.Evaluate (probably because I hate programming Chart objects), but according to Help it seems you can do interesting things with it:
“Chart Objects. You can specify any chart object name, such as “Legend”, “Plot Area”, or “Series 1”, to access the properties and methods of that object. For example,
Charts("Chart1").Evaluate("Legend").Font.Name returns the name of the font used in the legend.”
Evaluating Array Formulas
Amazingly if you give Evaluate an array formula it evaluates it as an array formula:
Sub FindXY_COUNTIFS4() Dim j As Long Dim dTime As Double dTime = Microtimer j = ActiveSheet.Evaluate("SUM((A1:A100000=" & Chr(34) & "x" & Chr(34) & ")*(B1:B100000=" & Chr(34) & "y" & Chr(34) & "))") Debug.Print "COUNTIFS4 " & j & " " & (Microtimer - dTime) * 1000 End Sub
This is quite a lot slower than COUNTIFS: it takes nearly 19 milliseconds.
If you are a SUMPRODUCT fan you could use
j = ActiveSheet.Evaluate("SUMPRODUCT(--(A1:A100000=" & Chr(34) & "x" & Chr(34) & "),--(B1:B100000=" & Chr(34) & "y" & Chr(34) & "))")
But its not significantly faster.
Evaluate speed compared to a formula in a cell
You would expect Evaluate to be slower than Excel natively calculating the formula in a cell. And indeed it is, but its quite close for a single formula;
Countifs Formula 6.0
Evaluate Countifs 6.1
Array Sum Formula 16.9
Evaluate Array Sum 18.9
Evaluate speed compared to using Application.Worksheetfunction
It looks like there is a higher overhead to using Evaluate, which is what you would expect.
Here is a version of the FindXY sub using Evaluate with MATCH instead of Worksheetfunction.Match.
Dim oRng As Range
Dim oLastRng As Range
Dim j As Long
Dim jRow As Long
Dim n As Long
Dim Rw As Long
Dim dTime As Double
dTime = Microtimer
Set oRng = Range(“a1:A100000”)
Set oLastRng = oRng(oRng.Rows.Count)
Rw = oLastRng.Row
On Error GoTo finish
Set oRng = Range(oRng(j + 1), oLastRng) ‘<<<<= Rw
j = ActiveSheet.Evaluate(“Match(” & Chr(34) & “X” & Chr(34) & “,” & oRng.Address & “, 0)”)
jRow = jRow + j
If oRng(j, 2).Value2 = “Y” Then n = n + 1
Loop Until jRow + 1 > Rw
Debug.Print “MatchEval ” & n & ” ” & (Microtimer – dTime) * 1000
This takes 3720 milliseconds compared to 478 milliseconds using Worksheetfunction.Match. There are just over 50000 calls to Evaluate or Match so I reckon the additional overhead of using Evaluate is about 65 Microseconds per call.
More Evaluate Limitations: Updated
- The string being evaluated must be less than 256 characters, even in Excel 2010.
- A1 style references can be evaluated in both A1 and R1C1 reference mode (Application.ReferenceStyle), but R1C1 style references can only be evaluated in R1C1 mode.
- Relative references in the string are treated as absolute, unless they are contained in defined names in which case the defined name is evaluated with respect to cell A1.
- Dates should be in USA format (Month-Day-Year).
- Evaluate will return an error value if the string formulae contains external references to closed workbooks or XLM functions.
- Using Evaluate INDEX(rng,rownum,COLUMN()) gives incorrect answers except for the first column. Evaluate 0+INDEX(rng,rownum,COLUMN()) works
- If the string formula contains a reference to both a UDF and a name it will fail with an error 2029 if the name reference occurs AFTER the UDF reference:
- If fred is a named range and xyz() is a user defined VBA function then this statement returns error 2029: application.Evaluate(“=xyz(b1)+fred”)
- This statement returns the correct value: application.Evaluate(“=fred+xyz(b1)”)
- Microsoft KB article 823604 identifies this problem but does not correctly diagnose the circumstances that cause it.
You can bypass many of these limitations (at the cost of performance) by inserting the formula string into a worksheet cell and then reading the resulting cell value back into a VBA variable.
If Evaluate cannot evaluate the formula string it returns an error rather than raising an error, so the result of Evaluate should always be assigned to a Variant.
- The Evaluate method adds a lot of power to VBA
- Always use Worksheet.Evaluate rather than Application.Evaluate: its twice as fast and less error-prone
- Using Worksheet.Evaluate has comparable speed to a cell formula but a higher overhead
- Worksheetfunction has a lower overhead than Evaluate
- Beware the Quirks of Evaluate!
So whats your experience of Evaluate?