Does .Value = .Value act similar to Evaluate() function in VBA?

﹥>﹥吖頭↗ 提交于 2019-12-19 03:18:22

问题


Consider the following snippet. It writes the same formula to two cells A1 and A2

Sub Main()
    With Range("A1")
        .Formula = "=1+1"
    End With

    With Range("A2")
        .Formula = "=1+1"
        .Value = .Value
    End With
End Sub

The second with block uses .Value = .Value which calculates/executes the formula therefore the formula disappears from the formula bar. Refer to hiding formulas from formula bar for a supportive reference.

Now, add another with block

With Range("A3")
     .Formula = "=1+1"
End With
Range("A4") = Evaluate(Range("A3").Formula)

You add a formula to the cell A3 then that new cell's formula is being Evaluated() into another cell A4. The results as shown

I think the above kind of shows that .Value = .Value and Evaluate() do the same thing.

However, The below code pulls value out of a closed workbook using the two mentioned approaches. I have created a workbook book9.xlsm for this example with a hello put in cell A1. book9.xlsm is the one I will be pulling the A1's value from. Consider the code

Sub PullValue()
    With Range("A1")
        .Formula = "='C:\Users\admin\Desktop\[book9.xlsm]Sheet1'!A1"
    End With

    With Range("A2")
        .Formula = "='C:\Users\admin\Desktop\[book9.xlsm]Sheet1'!A1"
        .Value = .Value
    End With

    Range("A3").Formula = "='C:\Users\admin\Desktop\[book9.xlsm]Sheet1'!A1"
    Range("A4") = Evaluate(Range("A3").Formula)
End Sub

The first with block puts a formula in cell's A1 value from a book9.xlsm. It gets executed therefore the pulled value is hello but the formula bar shows the actual .Formula which is C:\....

The second with block uses the .Value = .Value as demonstrated above for the evaluation of the formula and hiding the formula by replacing it with the result.

Range("A3") is the same as the first with block.

And now (A4) I am following the same principle as the first example(first snippet in this question) to Evaluate() the formula however It does not work this time.

Please see all activated cells values and formula bar for each one

So now, I cannot say that .Value = .Value is equal to Evaluate().

The remarks of Evalutate() say that it can be used with formulas. But in the example I have shown it doesn't work.

Are the formulas used as parameters in Evaluate() restricted? I've always thought that Evaluate is very powerful but it kind of turns out that .Value = .Value actually is even more powerful. Even though they are very similar they are somehow different ( but I am considering it might be my fault as the formula I chose for this example may be restricted or limited ). I think I have shown what makes them two similar and different at the same time. It still is like 50%/50% and I cannot tell exactly if they are the same or not. It would be great if somebody was able to explain what's missing here.


回答1:


.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




回答2:


.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:

  1. 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().

  2. 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.)




回答3:


.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.




回答4:


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.



来源:https://stackoverflow.com/questions/17745656/does-value-value-act-similar-to-evaluate-function-in-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!