81

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()

3 Answers 3

104

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.

Sign up to request clarification or add additional context in comments.

11 Comments

If you need to prefix with the name of the module, you have a problem. It should not be necessary.
@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.
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 - 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.
@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?
|
1

I agree with the recommendation to use the module name as part of the Call operation. While not strictly necessary if the name happens to be unique today within the project, it probably makes your code more robust against future changes if you are more explicit.

Main in Module1:

Public Sub MAIN()
    Call Module2.IDLE
End Sub

The original problem statement did not specify. but did imply both Module1 and Module2 would be in the same project. Assuming these modules are both in the same project it may be a good idea to add "Option Private Module" in Module2. When using VBA in an application like Word this can prevent IDLE from showing up as a user selectable/runnable macro in the UI.

IDLE in Module2:

Option Explicit
Option Private Module

Public Sub IDLE()
    ' Do something ... being idle requires being proactive.
    ' Add a delay here
End Sub

Extrapolating a little on why you might want to do that... One Word related use case is to have Module2 in a .dotm file, reference that .dotm file from several other projects (including the one containing Module1), and call code from Module2 from those other files. This facilitates writing/maintaining code in one place and reusing it where needed. If that is your scenario then you would not want to add "Option Private Module" because it would hide Module2 from Module1.

As mentioned in the comments, the use of Public is optional. My example above reflects me favoring being explicit about things like that.

1 Comment

1. You only 'need' to use the Module name when there are multiple members with the same name i.e. in different Modules ... however it is, IMHO, good practice to always use the Module name. 2. You do not need to use Public ... Subs are Public by default. 3. You can use Option Private Module to hide the Sub from the user interface. 4. You could use a Class Module (instead of a Standard Module) ... but you haven't explained how to then access the Sub in that Class Module.
0
Sub CreateWanderLensSlide()
    Dim pptApp As Object
    Dim pptPresentation As Object
    Dim slide As Object
    Dim shape As Object

    ' Initialize PowerPoint application
    Set pptApp = CreateObject("PowerPoint.Application")
    pptApp.Visible = True
    Set pptPresentation = pptApp.Presentations.Add

    ' Add a new slide
    Set slide = pptPresentation.Slides.Add(1, ppLayoutText)
    slide.Design = pptPresentation.Designs(1)

    ' Set slide title
    Set shape = slide.Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 50, 600, 50)
    shape.TextFrame.TextRange.Text = "WanderLens: See the World Through a New Lens 🌍✨"
    With shape.TextFrame.TextRange
        .Font.Name = "Arial Black"
        .Font.Size = 36
        .ParagraphFormat.Alignment = ppAlignCenter
    End With

    ' Add subheader
    Set shape = slide.Shapes.AddTextbox(msoTextOrientationHorizontal, 150, 120, 500, 30)
    shape.TextFrame.TextRange.Text = "Homepage Design Concept"
    With shape.TextFrame.TextRange
        .Font.Name = "Arial"
        .Font.Size = 20
        .Font.Color = RGB(0, 128, 255) ' Blue color
        .ParagraphFormat.Alignment = ppAlignCenter
    End With

    ' Add main content box
    Set shape = slide.Shapes.AddTextbox(msoTextOrientationHorizontal, 50, 180, 650, 300)
    shape.TextFrame.TextRange.Text = _
        "1. Navigation Bar: Home | Destinations | About Us | Contact" & vbCrLf & vbCrLf & _
        "2. Hero Section: 'Explore Now' button with immersive VR of a tropical beach." & vbCrLf & vbCrLf & _
        "3. Destination Previews: Interactive cards showcasing popular locations like Paris, Tokyo, and Bali." & vbCrLf & vbCrLf & _
        "4. Testimonials: Customer feedback carousel, e.g., 'Thanks to WanderLens, I explored Bali before booking!'" & vbCrLf & vbCrLf & _
        "5. Partner Offers: Exclusive travel discounts and partner promotions." & vbCrLf & vbCrLf & _
        "6. Footer: Privacy Policy, Social Media Links, Contact Info."
    With shape.TextFrame.TextRange
        .Font.Name = "Calibri"
        .Font.Size = 16
        .Font.Color = RGB(50, 50, 50) ' Dark gray
        .ParagraphFormat.Alignment = ppAlignLeft
    End With

    ' Add image placeholder for homepage screenshot
    Set shape = slide.Shapes.AddPicture("C:\Path\To\Your\Image.png", _
        msoFalse, msoCTrue, 50, 500, 650, 300)

    ' Finalize slide layout
    With slide.Shapes
        ' Align and distribute objects as needed
    End With

    MsgBox "Slide created successfully for WanderLens Homepage Concept!", vbInformation
End Sub

1 Comment

I'm not sure I see the connection between your example code and the original problem statement. If the connection is hidden in there somewhere it may be a good idea to add some explanation along with the example code so people can find the nugget you are trying to share with them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.