Nimrod
Nimrod

Reputation: 2413

Call a Subroutine from a different Module in VBA

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

I have the following code:

Sub MAIN()
    Call IDLE
End Sub

Upvotes: 80

Views: 318184

Answers (3)

RAJVI PATEL
RAJVI PATEL

Reputation: 11

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

Upvotes: 1

MustardMan
MustardMan

Reputation: 101

I agree with the need to use the module name as part of the Call operation.

Call Module2.IDLE

One other thing you will need to do is to make that other subroutine accessible.

Public Sub IDLE()

One of the potential drawbacks of putting it in a separate module is that in order to make them accessible to your code you need to declare them as public. This makes all of those public subroutines available to the user in the list of macros. At best this is just clutter for the user. It also opens the door, however, for users to call subroutines without the context of when and how to use them. If your subroutine is not safe to expose directly to the user then putting it in a module as a public subroutine is probably not the best solution.

If appropriate you could use a class module instead of a regular module. You can then expose the appropriate subroutines to your using macros without also exposing them directly to the user in the list of macros.

Upvotes: -1

dcp
dcp

Reputation: 55424

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.

Upvotes: 103

Related Questions