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
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
You can call them like

Code:
Sub MyBigSub()
Call Macro1
Call Macro2
End Sub
 
Upvote 0
If you want the code to DO something, write a Sub (called e.g. MySub) and from your main sub, call it using

Code:
Call MySub

If you want to perform a calculation, consider writing a Function Sub.

Remember that if you want to share variables between the different subs you'll have to declare them publicly outside of the subs themselves.

Otherwise, I'd recommend the Mr Excel VBA Macros book!
 
Upvote 0
Thanks for your suggestions. Having "gone" into the second sub and carried out the code there, how do I link back to the first sub at the place where I left it?
 
Upvote 0
You don't have to. When the called sub hits its End Sub it returns to the calling sub.
 
Upvote 0
Thanks very much. That's great and very helpful.
I note Yard's comments but (sorry I am stupid) what is the difference between "doing something" and performing a calculation? Is it simply, if I am doing "arithmetic" I use function, otherwise always use a sub?
 
Upvote 0
If you are doing arithmetic you could use either but a function would be better practice and probably easier to code e.g.

Code:
Function Cube(a As Double)
Cube = a ^ 3
End Function

Then in your sub

Code:
y = Cube(x)

If you want to do things like changing formatting, adding sheets and so on then you have to use a sub.
 
Upvote 0
Thanks - dont really understand what a function is as only ever written subs but it sounds that if I stick with subs I should be OK.
 
Upvote 0
Functions aren't that much different than subs. Here's an example of how to use either to perform the same thing:

Code:
Sub Area_Using_Sub()

Dim Width As Double, Length As Double


    Width = 4
    
    Length = 5
    
    MsgBox Width * Length


End Sub

Sub Area_Using_Function()


    MsgBox Area(4, 5)


End Sub

Public Function Area(Width As Double, Length As Double) As Double


    Area = Width * Length


End Function

The cool thing about a function (also called a UDF standing for User-Defined Function) is that you can use it like a formula within your cells. You have to declare the function as Public in order to do so.

Try this in one of your cells:

=AREA(6,7)
 
Upvote 0
Phantom,
I think I understand what you are saying, but I need to walk before I run and it's getting a bit complex for a newbie!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,322
Members
452,635
Latest member
laura12345

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