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!

Declaring Your Variables

VBA is a lazy programmers dream. You can take your variable name and assign practically any data to it. But it is a good habit to declare your variables which means indicating what type of data will be contained in them. This will help by taking less memory and speeding up execution time. But the most compelling reason is that it helps tremendously with one of the most common problems in programming, typos. If you declare your variable name, then misspell it later on, you’ll get an error before your program will even run rather than having the code execute with a zero or some other random value in a variable that should hold something else . This makes debugging much easier.

Like a lot of things, sometimes the best way to encourage a good habit is to force yourself to do it. You can force yourself to declare your variables by going to Tools>Options>Editor Tab in the VBE and checking the box for “Require Variable Declaration”. This adds two words to the top of every code module, “Option Explicit”. Ok, so I suppose now you’re going to want to know how to declare variables since you’ve have no choice!

Scope

First, a little discussion about variable “Scope”. The scope of a variable determines which code modules or procedures the variable’s contents will be accessible to. There are essentially three different variable scopes and they are summarized in Table 2:

Table 2: Variable Scope

Scope

How Variable is Declared

Single procedure

Dim statement within the procedure (Sub or Function)

Single Module

Dim or Private statement before the first procedure in a module

All Modules

Public statement before the first procedure in a module

The best way to help you understand is with a simple little example. This code could stand on its own in a Module:

Listing 7: Variable scope example

Option Explicit







Const PI As Single = 3.14159265359 'Constants work for all Modules



Dim strFile As String 'This Module only



Public lngCount As Long 'All Modules







Sub Test()



'Variable Declaration – Single procedure, ie. the values are only



'accessible within the “Test” subroutine.



'This is the most common way to declare your variables



Dim blnTrueFalse As Boolean



Dim intX, intY As Integer



Dim sngCalc As Single



Dim strName As String



Dim varUnknown As Variant



Dim varDefault 'A Variant by default







'Begin rest of your code below







'Finished, now exit the Sub



End Sub



Info: If you omit the “as VariableType” portion of the declaration statement, VBA implicitly assigns it as a data type Variant as seein near the bottom of Listing 7 above.



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.