如何在 VBA excel 中运行/调用另一个模块中的模块?

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

How do you run/call a module in another module in VBA excel?

excel-vbamodulecallvbaexcel

提问by Nur Atiqah Hassan

Okay so i have 3 modules. My first module is "main" that is the module that i need to call the other module. What i mean is that when i run main, i want it to call/runthe other 2 module. Currently when i press F5, it doesn't do this. How do i do this?

好的,所以我有 3 个模块。我的第一个模块是“main”,这是我需要调用另一个模块的模块。我的意思是,当我运行 main 时,我想要它到call/run其他 2 个模块。目前,当我按 F5 时,它不会这样做。我该怎么做呢?

My current code looks like this:

我当前的代码如下所示:

Sub main_TRY()
    Call Module2
    Call Module3
End Sub

All help is greatly appreciated. Thank you.

非常感谢所有帮助。谢谢你。

回答by Nur Atiqah Hassan

Answer:

回答:

Sub main_TRY1()
    Call Module2.Formating
    Call Module3.Data
End Sub

回答by Rycket

I have a simular struction on one of my vba-scripts, and it goes like this.

我的一个 vba 脚本上有一个类似的结构,它是这样的。

Private Sub CommandButton1_Click()
Call GetData1
End Sub

and in my module2, it looks like this

在我的模块 2 中,它看起来像这样

Sub GetData1()
'my code
Dim IE As Object
Dim dd As Variant

Set IE = GetObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}")

IE.Visible = False

IE.Navigate "https://www.avanza.se/aktier/om-aktien.html/5247/investor-b"

Application.StatusBar = "Loading, Please wait..."

IEWait IE

Application.StatusBar = "Searching for value. Please wait..."
dd = IE.Document.getElementsByClassName("lastPrice SText bold")(0).innerText
Range("G7").Value = dd
End Sub

Maybe a bit to much, but I just wanted you to understand the structure of the code I'm using.

也许有点太多了,但我只是想让您了解我正在使用的代码的结构。

So everytime I click the buttom "CommandButton1" This code above will execute, but in your case everytime you call the function main_try() , you should, as pointed out above, call on the functions in the modules, not the module itself.

所以每次我点击按钮“CommandButton1”时,上面的代码都会执行,但在你的情况下,每次调用函数 main_try() 时,如上所述,你应该调用模块中的函数,而不是模块本身。

Hoped it helped, best regards

希望它有所帮助,最好的问候