Call a Subroutine from a different Module in VBA

256,835

Prefix the call with Module2 (ex. Module2.IDLE). I'm assuming since you asked this that you have IDLE defined multiple times in the project, otherwise this shouldn't be necessary.

Share:
256,835
Admin
Author by

Admin

Updated on August 18, 2021

Comments

  • Admin
    Admin almost 3 years

    Is it possible to call a function from one Module to another?

    I have the following code:

    Sub MAIN()
        Call IDLE
    End Sub
    
    • MAIN is located in Module1
    • IDLE is located in Module2 and defined as: Sub IDLE()
  • dcp
    dcp about 14 years
    You're welcome. Please don't forget to accept the answer (click the check) :).
  • Fionnuala
    Fionnuala about 14 years
    If you need to prefix with the name of the module, you have a problem. It should not be necessary.
  • dcp
    dcp about 14 years
    @Remou - You are wrong. First, the OP already said it solved his problem, not sure why that wasn't enough for you. Secondly, and you can try this yourself, create Module1 and Module2 and create a sub in each of them called Sub1. Then try leaving off Module1 or Module2 from the Sub1 name when calling it from a subroutine in Sheet1 and let me know what happens when you try Debug->Compile from the VBA menu bar.
  • Fionnuala
    Fionnuala about 14 years
    If you create two procedures with the same name, the project will not compile and the error should be corrected. The OP mentions two procedures, one called Main and one called Idle. It should be possible to call Idle from Main, or even from the immediate window without prefixing it with anything, if these are two standard modules. If they are not, information is missing and the question is incomplete. It is important that answers should be valid for other people wishing to find an answer.
  • dcp
    dcp about 14 years
    @Remou - I just created two modules, module1 and module2, and created a subroutine called Sub1 in module1 and module2, and the project compiled fine. Did you try what I suggested yourself? Why do you say the project will not compile? I assumed here (maybe incorrectly, I updated my answer to reflect this assumption) that OP had same sub declared in 2 modules which is why they were having the problem. But again, I still don't understand why you don't think you cannot have 2 procedures with the same name, that's just plain wrong.
  • Fionnuala
    Fionnuala about 14 years
    Run the procedure from the immediate window. You should get "Compile Error. Ambiguous name detected: Idle."
  • dcp
    dcp about 14 years
    @Remou - Right, and that error goes away if you prefix the call with module1 or module2, which is the whole point I was making. But you said, "If you create two procedures with the same name, the project will not compile", which is not true. You can have 2 subroutines with the same name, it's just that when calling the sub you have to prefix with the proper module. Make sense?
  • Fionnuala
    Fionnuala about 14 years
    I think the better solution is to rename the procedure to avoid the ambiguity, but that is not the point. The OP mention two modules with two different names and in such a case prefixing should not be necessary, as I originally stated.
  • mchen
    mchen almost 11 years
    Does this also work with Application.Run? I.e can I do Application.Run(Module1.Foo). If so, what if also Module1 is an external reference and so exists in a separate file?
  • dcp
    dcp almost 11 years
    @Milo Chen- I'm not sure, you'd have to try it and see.
  • David Morin
    David Morin almost 10 years
    I don't quite understand why, but this fixed it for me as well.