加快这个vba脚本

时间:2016-08-13 19:50:46

标签: excel vba performance excel-vba

打开此工作簿时,将运行以下脚本,该脚本编写公式以创建指向多个工作簿的链接。每个工作簿写入3列,即工作簿1写入A,B和C,工作簿2写入D,E,F等。

WBNAME = "=IF(AND('\\insurance\it\FileData\Computers\Release Note\[Aliens.xlsm]iForms'!A2<>"""",'\\insurance\it\FileData\Computers\Release Note\[Aliens.xlsm]iForms'!E2=""""),'\\insurance\it\FileData\Computers\Release Note\[Aliens.xlsm]iForms'!A2,"""")"
Worksheets("All Release Items").Range("A2:A100").Value = WBNAME
WBNAME2 = "=IF(AND('\\insurance\it\FileData\Computers\Release Note\[Aliens.xlsm]iForms'!A2<>"""",'\\insurance\it\FileData\Computers\Release Note\[Aliens.xlsm]iForms'!D2<>"""",'\\insurance\it\FileData\Computers\Release Note\[Aliens.xlsm]iForms'!E2=""""),'\\insurance\it\FileData\Computers\Release Note\[Aliens.xlsm]iForms'!D2,"""")"
Worksheets("All Release Items").Range("B2:B100").Value = WBNAME2
WBNAME3 = "=IF('\\insurance\it\FileData\Computers\Release Note\[Aliens.xlsm]All Release Items'!A2<>"""",'\\insurance\it\FileData\Computers\Release Note\[Aliens.xlsm]All Release Items'!A1,"""")"
Worksheets("All Release Items").Range("C2:C100").Value = WBNAME3

目前有12个工作簿都是.xlsm,位于同一个文件路径中。 请帮忙

1 个答案:

答案 0 :(得分:0)

部分问题是需要将公式更新为在应用程序会话中未打开的引用工作簿。如果打开工作簿,它可能会更快,然后输入公式。

您可以通过将计算模式设置为手动(在&#34;计算&#34;部分&#34;公式&#34;功能区页面或Application.Calculation = xlCalculationManual)中进行测试,然后运行宏,那么&#34;现在计算&#34; (再次在功能区上,或F9),看看需要多长时间。比较运行它的时间,就像引用的工作簿打开和关闭时一样。我认为您会发现宏需要很短的时间来添加公式,但计算需要很长时间,因为工作簿已关闭。

如果是这种情况,请在计算之前重写它以打开工作簿。

此外,如果需要像这样引用数据,您可能需要考虑将数据放入数据库中。