如何制作我可以在 Excel 中的 VBA 中访问的库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1639590/
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-11 10:55:29  来源:igfitidea点击:

How to make a library I can access in VBA in Excel

excelvba

提问by Anthony

I am doing a lot of similar tasks among some VBA scripts I am writing and would like to develop a library (a bunch of convenience functions using the typelib I'm working with) which I can call from all my various scripts. I am new to the VBA world and do not know how this is done and have had a surprisingly hard time trying to figure it out.

我在我正在编写的一些 VBA 脚本中做了很多类似的任务,并想开发一个库(使用我正在使用的 typelib 的一堆便利函数),我可以从我所有的各种脚本中调用它。我是 VBA 世界的新手,不知道这是如何完成的,并且试图弄清楚这一点非常困难。

回答by Anthony

I think what I was looking for is an Add-In. Thanks everyone for your information.

我想我正在寻找的是一个加载项。谢谢大家的信息。

回答by George Sisco

Does this example help? It appears to also have a fix to a common issue when setting this up - You didn't mention which version of office - But 2003 is mentioned in that thread, so should be the fairly straight forward common case.

这个例子有帮助吗?设置它时,它似乎也解决了一个常见问题 - 您没有提到哪个版本的 office - 但是该线程中提到了 2003,所以应该是相当直接的常见情况。

http://socko.wordpress.com/2008/06/01/vba-code-library/

http://socko.wordpress.com/2008/06/01/vba-code-library/

回答by Toby Allen

Another option you have though it depends on the nature of your functions, is to create a COM object that you can call from your VBA scripts.

尽管这取决于函数的性质,但您可以选择的另一个选项是创建一个可以从 VBA 脚本调用的 COM 对象。

You can easily create a COM object using many languages including Delphi and VB (old style) it is also possible in .NET though a little more involved. You can then do your calculations in your COM object and even pass in the excel worksheet etc you wish to manipulate if required.

您可以使用包括 Delphi 和 VB(旧样式)在内的多种语言轻松创建 COM 对象,但也可以在 .NET 中使用,但要多一些。然后,您可以在 COM 对象中进行计算,甚至可以在需要时传入您希望操作的 Excel 工作表等。

Depending on the nature of your functions this may or may not be useful.

根据您的功能的性质,这可能有用也可能没有用。

回答by EToreo

You will need to use VB to do this. With Microsoft plug-ins to VB, you can manipulate Excel files without even opening them, much like you do now. The code will be very similar once you have the file open.

您将需要使用 VB 来执行此操作。借助 Microsoft VB 插件,您甚至无需打开 Excel 文件即可操作它们,就像您现在所做的那样。打开文件后,代码将非常相似。

I would transfer all my code to VB and after its working like you have your VBA macros start making libraries out of the common stuff.

我会将我所有的代码转移到 VB,然后在它工作之后,就像你让 VBA 宏开始从常见的东西中制作库一样。

Lots of work, but if you really are doing a lot of this stuff, it will be great in the long run. (Job security too ;)

很多工作,但如果你真的做了很多这样的事情,从长远来看会很棒。(工作保障也是;)