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

129.5k views Asked by At

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/run the other 2 module. Currently when i press F5, it doesn't do this. How do i do this?

My current code looks like this:

Sub main_TRY()
    Call Module2
    Call Module3
End Sub

All help is greatly appreciated. Thank you.

3

There are 3 answers

2
Nur Atiqah Hassan On BEST ANSWER

Answer:

Sub main_TRY1()
    Call Module2.Formating
    Call Module3.Data
End Sub
0
Rycket On

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

Private Sub CommandButton1_Click()
Call GetData1
End Sub

and in my module2, it looks like this

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.

Hoped it helped, best regards

0
Thor On

I had a similar situation where I needed to call a "Private Sub" routine. I decided to go with having each module call to the next. This allowed me to call the first module, then it would run code and trigger the next module if successful, and so on.

Sub main_TRY()
    Application.Run ("ModuleNameHere")
End Sub

Just add the following to the end of each module and have it call to the next module you would like to run "NextModuleNameHere":

    Application.Run ("NextModuleNameHere")