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

前端 未结 4 1631
别跟我提以往
别跟我提以往 2021-01-06 00:33

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

Sub Main()
    With Range(\"A1\")
        .Formul         


        
4条回答
  •  伪装坚强ぢ
    2021-01-06 01:17

    .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 = "", where 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.)

提交回复
热议问题