Consider the following snippet. It writes the same formula to two cells A1
and A2
Sub Main()
With Range(\"A1\")
.Formul
.value and Evaluate are not the same.
Excel maintains both a value and a formula string for each used cell, and you can get both of these independently using Range.Value and Range.Formula.
When you use Application.Evaluate to evaluate a string the string is evaluated as a formula on the active worksheet (so actually its better to use Worksheet.Evaluate rather than Application.Evaluate, and its faster too).
Using Rng1.Value=Rng2.Value copies the value from Rng2 into Rng1 and overwrites the formula of Rng1.
Using Rng1.Value=Evaluate(rng2.Formula) asks Excel to retrieve the formula string from rng2, evaluate it and return the result to Rng1.
The Evaluate method does not work exactly the same way as a formula in a cell: it has many "quirks" that you need to be aware of (including the fact that it does not work with formulas referring to external closed workbooks): see my blog post for details
Also its generally better to use .Value2 rather than .Value: see Value vs Value2 for details
Evaluate is a function And something which use followed by a dot (.) after a function,that is called as property. So ".Value",".Formula",".Text" are properties of Range which you are using here.
Don't mix up these 2 things.
A function takes input, perform actions using its input variables and return results. And it works on the data type for which it is configured.
.value is a generic property which is not dependent on data type. It can be string, numeric, float or whatever.
so there is possibility where you get error from 1 and other work absolutely fine.
.Value = .Value
and Evaluate()
are different.
I think you have some confusion with objects and their default properties.
Always keep in mind these two concepts:
VBA allows you to use properties and methods without specifying the object. VBA will use their default object. For example when you use Evaluate()
you actually use Sheet1.Evaluate()
.
VBA allows you to use objects without specifying the property. VBA will use their default property. For example when you use Range("A1") = 1
you actually use Range("A1").Formula = 1
. (You actually use Sheet1.Range("A1").Formula = 1
!)
Going back to your example, when you do .Value = .Value
you actually do Range("A2").Value = Range("A2").Value
. The Value property of a cell can be a number, a string, an error, etc. And when it is a number, it could be the wrong number, that is not the correct result of the formula in that cell (for example because you have automatic calculation disabled). So .Value = .Value
is equivalent to .Formula = "<xxx>"
, where <xxx>
is the last value calculated on the cell.
When you do Range("A4") = Evaluate(Range("A3").Formula)
you ask Excel to evaluate the formula and assign the result to the Formula
property of the range A4 (because the formula property is the default property of a range object.)
.Value = .Value
(within a cell's With
block) simply sets the value of the cell to its current value, overwriting and removing its formula if it has one. The .Value
property simply represents the current value of a cell. If the cell contains a formula that has not yet been calculated, e.g. if calculation has been turned off, it may not return the formula result, instead returning the previous value of the cell.
Excel.Application.Evaluate
takes a string value, evaluating the string's contents as if it were a formula or the name of a cell, and returning the value of that cell or formula. If you pass it a string that can't be mapped to an Excel cell name or isn't a valid formula, you'll get an error. The purpose of Evaluate
is to allow dynamically created formulas to be evaluated without having to write the formula out to a cell. If passed a formula, it should also return a result even if workbook calculation is turned off, though if passed a name, I would expect it to return the current value of a cell, and if workbook calculation is off, that value may not reflect the expected value of the referenced cell.
Presumably since the .Value
of a cell and Evaluate()
can return different results, the worksheet formula evaluation engine and the Application.Evaluate()
engine are different, or at least have some different elements.