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

前端 未结 4 1674
别跟我提以往
别跟我提以往 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 00:58

    .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

提交回复
热议问题