我有一个 2010 Excel 64 位模型,它有一个 VBA 子例程来运行 16 种输入组合,所有这些组合都使用相同的 Excel 模型计算进行处理,然后将结果输出到模型中的选项卡。我可以访问高性能集群 (HPC) 并希望运行 VBA 代码,以便我可以并行运行 16 种组合,而不是当前在 HPC 上的顺序进程。我应该如何处理这个?例如,我是否需要将每个组合放入一个单独的子例程中并有一个主 VBA 子例程来调用每个组合?为了在 HPC 上运行模型,我需要包含前端和后端 VBA 代码吗?
1 回答
Excel VBA does not directly allow multithreading, so unfortunately there is no simple VBA solution for this.
I can see a couple options here, and it will depend on your problem whether you will be able to use them.
In Excel 2007 and 2010, worksheet functions can execute in parallel. If your VBA code is a function and not a sub, and if most of your data comes from the worksheet, you could try to take advantage of that.
You could write a DLL that handles multithreading yourself, and call it from Excel. For this, you'd have to port your code to VB 6 or VB.NET (or straight up rewrite it in C/C++), and manually deal with multithreading.