76

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()
Adam Jensen
  • 541
  • 1
  • 10
  • 25
Nimrod
  • 2,343
  • 3
  • 17
  • 7

1 Answers1

99

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.

dcp
  • 54,410
  • 22
  • 144
  • 164
  • If you need to prefix with the name of the module, you have a problem. It should not be necessary. – Fionnuala May 10 '10 at 19:18
  • 1
    @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. – dcp May 10 '10 at 19:24
  • 8
    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. – Fionnuala May 10 '10 at 19:43
  • @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. – dcp May 10 '10 at 19:53
  • Run the procedure from the immediate window. You should get "Compile Error. Ambiguous name detected: Idle." – Fionnuala May 10 '10 at 19:57
  • 1
    @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? – dcp May 10 '10 at 20:06
  • 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. – Fionnuala May 10 '10 at 20:15
  • 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? – mchen Jul 30 '13 at 23:03
  • @Milo Chen- I'm not sure, you'd have to try it and see. – dcp Jul 31 '13 at 12:28
  • I don't quite understand why, but this fixed it for me as well. – David Morin Jul 18 '14 at 12:41
  • "...otherwise this shouldn't be necessary. ...". Separation of concerns? – Psionman Aug 04 '22 at 14:32