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

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

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

提交回复
热议问题