简体   繁体   English

Excel VBA>在保存工作簿之前不更新公式

[英]Excel VBA > Formula not updating until workbook is saved

I have a very large Excel workbook (25+ worksheets, 15+ VBA modules) for financial analysis. 我有一个非常大的Excel工作簿(25个以上的工作表,15个以上的VBA模块)用于财务分析。

I've noticed that one of the formula doesn't display a value until I manually save the workbook 2 - 3 times. 我注意到,直到我手动保存工作簿2至3次,该公式之一才显示值。 Before saving it will appear as a dash (-). 在保存之前,它将显示为破折号(-)。 After saving 2 - 3 times, the values appear as expected. 保存2至3次后,值将按预期显示。

I have put in a few ActiveWorkbook.Save in my macro runs already but they don't seem to affect this case. 我已经放了一些ActiveWorkbook.Save已经在我的宏运行中运行了,但是它们似乎并没有影响这种情况。

The formula in question is (for one cell): 有问题的公式是(对于一个单元格):

=OFFSET(THIS_CELL, 0, -1)+INDEX('ExternalWorksheet'!$A$1:$CR$300,MATCH("Some Term",'ExternalWorksheet'!$A:$A,0),MATCH($B5,'ExternalWorksheet'!$1:$1,0))+INDEX('ExternalWorksheet'!$A$1:$CR$300,MATCH("Some Other Term",'ExternalWorksheet'!$A:$A,0),MATCH($B5,'ExternalWorksheet'!$1:$1,0))+INDEX('ExternalWorksheet'!$A$1:$CR$300,MATCH("Yet Another Term",'ExternalWorksheet'!$A:$A,0),MATCH($B5,'ExternalWorksheet'!$1:$1,0))-OFFSET(THIS_CELL, 0, -17)

(I know it is unruly, I didn't create it, I'm just the programmer ;) ) (我知道这是不守规矩的,我没有创建它,我只是程序员;)

I am using the THIS_CELL trick as described in this other SO post because this formula appears in multiple 'tables' in this worksheet. 我正在使用另一个SO帖子中所述的THIS_CELL技巧,因为此公式出现在此工作表的多个“表”中。

My client isn't pleased about having this column appear as dashes and the columns that depend on it appearing as N/A until he saves. 我的客户不满意让该列显示为破折号,而依赖该列的列显示为N / A,直到他保存为止。 From what I can tell, this behaviour is related to the usage of THIS_CELL (or the OFFSET) as if I remove the OFFSET the cells update as expected. 据我所知,此行为与THIS_CELL(或OFFSET)的使用有关,就好像我删除了单元格按预期更新的OFFSET一样。 However, I can't see how to get rid of it as I have to repeat this formula over multiple 'tables' in the worksheet and have them reference the correct cells. 但是,我看不到如何摆脱它,因为我必须在工作表中的多个“表”上重复此公式,并让它们引用正确的单元格。 I also can't switch to RC notation as my client doesn't like it. 我也不能切换到RC表示法,因为我的客户不喜欢它。

You can modify THIS_CELL trickery to remove dependency on the OFFSET function altogether by changing the initial selected cell to produce the offset necessary. 您可以通过更改初始选定的单元格以产生必要的偏移量来修改THIS_CELL技巧,以完全消除对OFFSET函数的依赖。 The reason that the first step in those directions states select cell A1 (this is important!) is so THIS_CELL always refers to the cell you selected. 在这些方向上第一步说明选择单元格A1的原因(这很重要!) ,因此THIS_CELL始终引用您选择的单元格。 You don't want the cell that you selected. 您不需要所选的单元格。 You want the one either 1 or 17 columns to the left. 您想要左边的1列或17列。

(stolen and modified from Create a named formula called THIS_CELL ) (从创建名为THIS_CELL的命名公式窃取并修改)

  1. In the current worksheet, select cell R1 (this is important!) 在当前工作表中,选择单元格R1 (这很重要!)
  2. Open Name Manager (Ctl+F3) 打开名称管理器(Ctl + F3)
  3. Click New... 点击新建...
  4. Enter "THE_CELL_BEHIND_ME" into Name: 在名称中输入“ THE_CELL_BEHIND_ME”:
  5. Enter the following formula into Refers to: =!Q1 在“引用”中输入以下公式: =!Q1

NOTE: Be sure cell R1 is selected. 注意:确保已选择单元格R1 This formula is relative to the ActiveCell. 此公式相对于ActiveCell。

  1. Under Scope: select Workbook. 在范围下:选择工作簿。

  2. Click OK. 单击确定。

  3. Repeat using the name THE_CELL_WAY_BEHIND_ME and =!A1 for refers to. 重复使用名称THE_CELL_WAY_BEHIND_ME和=!A1作为引用。

  4. Close the Name Manager. 关闭名称管理器。

Using =THE_CELL_BEHIND_ME is the same as =OFFSET(THIS_CELL, 0, -1) and =THE_CELL_WAY_BEHIND_ME is the same as =OFFSET(THIS_CELL, 0, -17) . 使用=THE_CELL_BEHIND_ME=OFFSET(THIS_CELL, 0, -1)相同,并且=THE_CELL_WAY_BEHIND_ME=OFFSET(THIS_CELL, 0, -17)

I did a comparsion between CTRL+S save and a ActiveWorkbook.Save and in this case they do work differently. 我在CTRL + S保存和ActiveWorkbook.Save ,在这种情况下,它们的工作方式有所不同。 No matter how many times I save via VBA it wouldn't update the formulae. 无论我通过VBA保存多少次,都不会更新公式。 CTRL+S appears to be forcing a re-calculation. CTRL + S似乎正在强制重新计算。

From this post I added in 在这篇文章中,我添加了

ActiveSheet.EnableCalculation = False  
ActiveSheet.EnableCalculation = True

prior to saving and that has solved the problem. 在保存之前已经解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VBA,每天在公式中更新到其他工作簿的链接 - VBA, Updating link to other workbook in formula daily Excel VBA-已在宏中循环保存的工作簿…工作簿无法打开 - Excel VBA - Saved Workbook in loop within macro…Workbook fails to open Excel VBA公式的不更新行 - Excel VBA formula's not updating row 保存在网络驱动器上时,Excel Workbook外部链接不会更新。 - Excel Workbook External Links not updating when saved on network drive. Excel不更新单元格,选项>公式>工作簿计算设置为自动 - Excel is not updating cells, options > formula > workbook calculation set to automatic DoWhile循环中vlookup公式中的Excel VBA引用命名为工作簿 - Excel VBA reference named workbook in vlookup formula in DoWhile loop 暂停执行VBA脚本,直到打开另一个特定的Excel工作簿? - Pause execution of VBA script until another specific excel workbook opens? Excel VBA循环直到空白单元格并将工作表复制到新工作簿 - Excel VBA loop until blank cell and copy worksheet to new workbook Excel VBA 2010:用VBA保存工作簿时,数据验证中断,但手动保存时,数据验证不中断 - Excel VBA 2010: Data validation breaks when workbook saved by VBA, but not when saved manually Excel-Vba:应用公式直到最后一行不起作用的代码 - Excel-Vba : Code for Applying Formula until Last Row not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM