Member Login

Login
No account yet? Register
 

Search GeoPrac

GeotechSearch.com

Site Sponsors

Even More News and Blog Posts!

New on GeoPrac.net, the Other News and Blogs page has had a major facelift. To supplement our regular GeoNews and Articles, we aggregate news feeds and blog entries from all over the web into one easy to browse source. Check it out!  

Become an Author

GeoPrac.net is a community site, we are only as good as the content our members contribute!

Whether it's a one time contribution, or a monthly or quarterly article, please consider becoming an author

Subscribe by Email

Subscribe to GeoPrac.net Headlines by Email. Powered by FeedBurner.com, Don't forget to follow instructions to verify your subscription!

Enter your email address:

RSS Feeds

To subscribe to GeoPrac.net headlines, use one of the buttons below. Or view other available feeds or read more about RSS.

GeotechSearch.com Most Popular Search Terms

Home arrow Articles arrow Browse by Category arrow Software and Spreadsheets arrow VBA and Excel for Engineers and Scientists – Part 2
VBA and Excel for Engineers and Scientists – Part 2 - Introduction Print E-mail
Written by Randy Post   
Tuesday, 20 May 2008
Article Index
Introduction
Code Containers
Subroutiness and Functions
Variables and Data Types
Declaring Your Variables
Variable Expressions and Math
Other Functions
Flow Control
Help and Debugging
Custom Spreadsheet Functions
Examples and Downloads
Wrap-up and Recommended Books
Digg!Reddit!
Del.icio.us!Google!
Facebook!Slashdot!
Technorati!StumbleUpon!
Newsvine!Furl!
Yahoo!Ma.gnolia!

Subroutines and Functions

Pretty much every programming language has subroutines and functions. Both hold pieces of your overall code and work essentially the same except that with a function, you typically want to return a value or an array of values. There are ways of returning values with Subroutines also, but I don’t want to get ahead of myself. Another difference is that you can define a function in your code module that will act just like a built-in Excel formula. More on that later.

To help you understand the difference between Subroutines or “Sub” procedures and Functions, I borrow the following paragraph from Walkenbalk (2004a):

“You can think of a Sub procedure as a command that can be executed either by the user or by another procedure. Function procedures, on the other hand, usually return a single value (or an array), just like Excel worksheet functions and VBA built-in functions do. Like with built-in functions, your Function [and Sub] procedures can use arguments.”

Clear as mud? That didn’t help explain it as much as I though it would. I’ll try to illustrate the basic structure of a subroutine or a function with a couple simple examples.

Listing 1: Subroutines vs. Functions

Sub NameOfSubroutine(Arguments)



'Code goes here



End Sub







Function NameOfFunction(Arguments) AS ReturnDataType



'Code goes here



NameOfFunction = ReturnValue



'The previous line sends the value back to the calling cell or procedure



End Function



Where you see “Arguments” above, it can be left blank for no arguments, or you can have one or multiple arguments. Each argument is essentially a variable name and you declare it in a similar way to a variable as well. These variables will be available to use within the subroutine or function but not outside of it. Take a look at Listing 2 below to see what I mean. The “Single” means single precision floating point number, the data type. We will discuss data types in a moment.

Listing 2: Function with multiple arguments

Function MultiplyExample(Value1 As Single, Value2 As Single) As Single

Now let me show you how to call a Sub Procedure from another function or Sub procedure. Below that, I will show how to call a function and get a value back.

Listing 3: Calling functions or subroutines

Sub TestFunctionCall()



'Note how there are no arguments to this TestFunctionCall Sub











'Here is the Sub Call on the next line



Call NameOfSubToCall(Arguments)







'Here is a call to a function



ResultVariable = NameOfFunctionToCall(Arguments)



'We will discuss variables in the next section



End Sub

While writing a code, make liberal use of comments in your code or you’ll never be able retrace your steps when you come back to it a few days, weeks or months later. In VBA, a comment line begins with a single quote mark. In addition to annotating your code, you can use comments to “comment out” lines that you suspect may be causing errors when you are debugging.

One other big difference between Subs and Functions is that if you have a Subroutine with no arguments, you can “Play” that subroutine just like you would a Macro that you recorded. In Excel 2003 and prior, you would play it using the Macro Toolbar or from the Tools Menu. In Excel 2007, you click on the Developer tab on the ribbon and then click the “Macros” button.



Trackback(0)
Comments (1)add comment

Ted said:

 
Very nice. I have avoided vba and macros up to this point but I think I'll give it a try.
Monday June 16, 2008 10:13AM

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley

security image
Write the displayed characters


busy
Last Updated ( Monday, 21 July 2008 )
 
< Prev   Next >

Site Sponsors

Users Online

No Users Online

Statistics

Members: 126
News: 366
Web Links: 127

GeoPrac.net © 2007 Randy Post unless noted.
Terms of Use | Copyright Info | Privacy Policy | Disclaimer | Sitemap

Other Randy Post Sites: RnRPost.com | NameDrawing.net 

Article Copyright

All content in the ARTICLES section of GeoPrac.net is copyrighted by their authors unless otherwise noted and reproduced here with permission. Refer to the copyright page or the terms of use for more information.