Linking subs

gregv

New Member
Joined
Nov 29, 2008
Messages
12
Hi,
I am teaching myself excel VBA and at present, everything I write, is done in one sub. There are certain sections of my code that I need to repeat several times in the sub. Is there a way I can put those lines in another sub (or area) which I could link into (and out of) each time I need that calculation carried out?
Sorry if it's an obvious or daft question! - Thanks
 
Here's another tidbid that you will most likely need if you plan on "linking" subs together. That is passing a variable from one sub to the next.

Code:
Sub Macro1()

Dim My_String As String


    My_String = InputBox("What's your name?", "ENTER YOUR NAME", "Billy")
    
    Call Macro2(My_String)
    
    MsgBox "That was thrilling, wasn't it?", vbQuestion + vbYesNo + vbDefaultButton2, "YEE HAW"
    
    
End Sub


Sub Macro2(Passed_Variable As String)


    MsgBox "Good day to you " & Passed_Variable


End Sub
 
Upvote 0

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Is it possible to Call an individual Sub inside of another Module?
say, from Module1... call the 2nd Sub in Module2?
 
Upvote 0
Absolutely. Just make sure that the sub isn't a Private sub

Public Sub Macro1 = Available anywhere in the project. Able to be seen in the macros list if you wish to play a macro from a worksheet using the Excel menu.

Sub Macro1 = Same thing as Public

Private Macro1 = Only available within the module. Not seen in the macros list.
 
Upvote 0
No need to include the module name in your call procedure. You cannot have two macros with the same name within the same project.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top