tags:

views:

117

answers:

1

Can I call a function from one Module to another?

I wrote the following:

Sub MAIN()
    Call IDLE
End Sub

"Main" is located in "Module1". "IDLE" is located in "Module2". (defined as: "Sub IDLE()")

Thanks.

can this be done?

+1  A: 

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
Thanks.it works :)
Nimrod
You're welcome. Please don't forget to accept the answer (click the check) :).
dcp
If you need to prefix with the name of the module, you have a problem. It should not be necessary.
Remou
@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
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.
Remou
@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
Run the procedure from the immediate window. You should get "Compile Error. Ambiguous name detected: Idle."
Remou
Here is a KB http://support.microsoft.com/kb/817411
Remou
@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
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.
Remou