vba 如何仅计算 Excel 中的活动工作表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31074471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:43:16  来源:igfitidea点击:

How to calculate only the active worksheet in Excel?

excelvba

提问by Clauric

I have a very large number of calculations across multiple worksheets (10+ sheets, 30000+ rows, 250+ columns, about 50% with dependencies).

我在多个工作表(10 多张工作表、30000 多行、250 多列、大约 50% 的依赖项)中进行了大量计算。

I have a VBA program that runs through a list of variables, changing them one at a time. This change the values in all the cells with formulae.

我有一个 VBA 程序,它运行一系列变量,一次更改一个。这会使用公式更改所有单元格中的值。

I don't need all the sheets recalculated every time I change a variable.

每次更改变量时,我都不需要重新计算所有工作表。

Is there a way to recalculate only the active worksheet or a specified worksheet?

有没有办法只重新计算活动工作表或指定的工作表?

回答by Miss Palmer

ActiveSheet.Calculate

or

或者

vbasheetname.Calculate

or

或者

Worksheets("Sheet1").Calculate

It's better practise to avoid using activesheetif possible in your code (there's only rarely a need for it), so try and stick to the second two.

activesheet如果可能的话,最好避免在您的代码中使用它(很少需要它),所以尽量坚持使用后两个。

回答by Ben

One way to do it without VBA is to make the formulas to be dependent upon a boolean cell. If the bool is true, then they calculate, otherwise they do not.

不使用 VBA 的一种方法是使公式依赖于布尔单元格。如果 bool 为真,则计算,否则不计算。

Then in this boolean cell, type this formula: =CELL("filename") = CELL("filename", [B1]). Then place a simple toggle button on this sheet that is linked to any cell in the worksheet. Then, whenever the toggle button is pressed, the linked cell will change values, which will cause the formula in the boolean cell to produce TRUE rather than FALSE, and all the cells that are dependent upon this boolean cell will then calculate.

然后,在这个布尔单元格,键入以下公式:=CELL("filename") = CELL("filename", [B1])。然后在此工作表上放置一个简单的切换按钮,该按钮链接到工作表中的任何单元格。然后,每当按下切换按钮时,链接的单元格将更改值,这将导致布尔单元格中的公式生成 TRUE 而不是 FALSE,然后所有依赖于该布尔单元格的单元格都将进行计算。

Example #1

示例#1

Example #2

示例#2

This won't keep the cells from calculating, but it will make their calculations super quick.

这不会阻止单元格计算,但会使它们的计算速度非常快。