Excel:如何从VbScript调用工作表模块中的函数

use*_*565 3 vbscript excel vba excel-vba

Using VBScript, I'm trying to run a subroutine that resides in one of the code modules. Now this particular sub calls several other subs/functions, including few that reside on the sheet modules. And on calling these subs residing in Sheet Modules, I get can error:

"Sub or Function undefined!"
Run Code Online (Sandbox Code Playgroud)

How can I get around this? There are a lot of such functions and I don't want to move all of them from Sheet modules to code modules. Is there a better work around for this?

VBScript that calls a sub (residing in Code modules) that does not call any code on Sheet modules is working fine, so I'm sure it is the above bit that is causing problem.

Sam*_*Sam 6

如果要从不同的模块调用子例程或函数,则需要确保两件事

  1. 子/功能不应声明为私有
  2. 通过在代码所在的模块名称前添加前缀来调用Sub / Function。

因此,如果您有一个Sub调用,TestSub并且它位于Sheet1的代码模块中,则应将其命名为Sheet1.TestSub。使用just调用它TestSub会导致错误Sub or Function not defined

如评论中所述,工作表的名称并不总是与显示名称相同。您可以从VBE中的对象资源管理器中找到工作表的名称

在此示例中,实际工作表名称在左侧,而显示名称在右侧。您应该使用左侧的名称来参考工作表。

工作表名称