Loading
Excel.Tips.Net ExcelTips (Menu Interface)

Understanding Subroutines

Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Understanding Subroutines.

When you write macros in Excel, you use a programming language called Visual Basic for Applications (VBA). This is based on the BASIC programming language, with extensions specific to Excel. One of the features of the language is the capability to use subroutines in your programs. For instance, consider the following VBA macro:

Sub Macro1()
    TestSub
End Sub
Sub TestSub()
    MsgBox "In the subroutine"
End Sub

This simple macro (Macro1) does nothing but call a subroutine (TestSub), which in turn displays a message box to inform you that it is in the subroutine. When you click on OK to dismiss the message box, the subroutine ends and returns control to the main program. You can have as many subroutines in a VBA program as you desire. The purpose of each should be to perform common tasks so you don't have to rewrite the same code all the time.

You can also pass parameters to your subroutines. These parameters can then be acted upon by your subroutine. For instance, consider the following macro:

Sub Macro1()
    A = 1
    PrintIt A
End Sub
Sub PrintIt(x)
    MsgBox "Value: " & x
End Sub

This is a simple macro that sets a variable, and then passes it in a subroutine call to PrintIt. This subroutine displays the value of the variable in a message box, and then (after you press OK) returns to the calling program.

Notice that the subroutine does not use the same variable name as it was passed. This is because VBA reassigns the value of x (what the subroutine expects to receive) so that it matches the value of A (what the program is passing to the subroutine). The important thing to remember in passing parameters to subroutines is that your program must pass the same number of parameters as the subroutine expects, and that the parameters must be of matching types and in the proper order.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2258) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Understanding Subroutines.

Related Tips:

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

 

Leave your own comment:

*Name:
Email:
  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*Text:
*What is 5+3 (To prevent automated submissions and spam.)
 
 
           Commenting Terms

Comments for this tip:

Barry    29 Nov 2015, 07:32
@Charles,

The environment within which macros/VBA codes executes is quite complex and probably beyond the scope of a short tip. VBA Subroutines have a scope, can be public or private, you can have as many parameters passed to a subroutine as you like, some can even be optional which can have default values. A subroutine can return a result in which case it becomes a function. Parameters can be passed "ByValue" or "ByRef". Any book on VBA will explain these terms, and their usage.

Using the above example I've expanded it to include four passed parameters (A, B, C, & D):

Sub Macro1()
    A = 1
    B=" text" 'note the space before the word
    C=" more text" 'note the space before the words
    D=" even more text" 'note the space before the words
    PrintIt A, B, C, D
End Sub

Sub PrintIt(x, y, z, w)
    MsgBox "Value: " & x*2 & y & z & w 'which will display "2 text more text even more text"
End Sub

Strictly speaking the result of x*2 is a number not a string and so is not best practice, but Excel does an implicit conversion to a string for use by the concatenate operator (other programming languages are not so forgiving and would give an error). If you wanted to be a purist you would use Cstr(x*2) to convert the number to a string.

You can add more parameters using the same principles shown above. It is good programming practice to also define the data type of the parameters being passed in this way:

Sub PrintIt(x as Integer, y as String, z as String, w as String)
    MsgBox "Value: " & x*2 & y & z & w
End Sub
Charles Stukins    29 Nov 2015, 04:45
Understanding subroutines of passing variables is very helpful. But what if I want to pass several variables to a sub routine from the main program and use them in the subroutine?
 
 

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us

 

Advertise with Us

Our Privacy Policy

Our Sites

Tips.Net

Beauty and Style

Cars

Cleaning

Cooking

DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)

Gardening

Health

Home Improvement

Money and Finances

Organizing

Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives

 

Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2016 Sharon Parq Associates, Inc.