VBA: cannot automatically recalculate Excel formula after updating it — needs manual interaction

柔情痞子 提交于 2019-12-12 18:46:30

问题


In VBA, I am updating the formula in a cell (which works ok), but automatic recalculation does not work:

updated_formula = "=COUNT(Sheet1!A3:A" & nr_points & ")"
Cells(x, y).Formula = updated_formula
ActiveWorkbook.Save
Cells(x, y).Calculate

The formula simply counts the number of existing rows in another sheet. When I run the macro, the cell's value in the function textfield is correct, but in the cell itself I have "#NAME?" and I need to press ENTER in the function to recalculate the formula.

Am I expecting too much of Excel? Or am I doing something wrong?

EDIT: Screenshot of the situation -- this is what I see after running the macro. (Sorry for the black censoring, have to maintain anonimity for the client company)


回答1:


After seeing your screenshot, it became clear.

Change

Cells(x, y).Formula = updated_formula

to

Cells(x, y).FormulaLocal = updated_formula




回答2:


Your code works fine on my sheet and updates calculated value immediately after macro run. Please make sure Calculation Options are set to "Automatic": Ribbon Data tab > Calculation Options > Automatic.




回答3:


I know this is a late post, but I had a similar issue and what worked for me was to change

Cells(x, y).Calculate

to

Cells(x, y).Dirty


来源:https://stackoverflow.com/questions/14361270/vba-cannot-automatically-recalculate-excel-formula-after-updating-it-needs-m

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