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!

Variables and Data Types

We started out discussing a container that will hold all of your code, a Module. Within your Excel Spreadsheet, you can have multiple Modules. Within each module you can have multiple Functions and Sub Procedures which are themselves containers of code in a sense. The smallest “container” is a variable which can contain a floating point number, an integer, a string of text, or a boolean (True/False) value among other things.

Before I get more into talking about what a variable is and how to work with it, let’s look at some of the most common things you will be storing in variables. Below is a table for the various data types.

Table 1: Data Types (From Walkenbach, 2004b)


Data Type

Naming Convention Prefix

Approximate Range of Values

Boolean

Bln

True or False

Integer

Int

± 32,000

Long (Integer)

Lng

± 2 x 109

Single (Precision Float)

Sng

± 3 x 1038

Double (Precision Float)

Dbl

± 1 x 10300

Date

Dte

January 1, 100 AD to December 31, 9999

Object

Obj

NA

String

str

65,000 characters

Variant

var

2 billion characters

The middle column from Table 1 is a suggested prefix for your variable’s name so that when you are coding, you can see what type of variable it is. This is not mandatory, but you may consider it to keep things tidy. At a minimum, use descriptive, unique variable names. Here are a few rules that VBA has for variable naming (After Walkenbalk, 2004a):

  • You can use alphabetic characters, numbers and some punctuation characters, but the first character must be alphabetic.
  • VBA variables are not case sensitive. For readability, it is suggested to use mixed case (MyVariable instead of myvariable)
  • No spaces or periods, underscore character is acceptable.
  • Watch out for VBA reserved words. If you use one of these words, you will get a syntax error. Reserved words appear in blue for the default settings in the VBE, they include words like “Then”, “Else”, and names of existing functions.

To see how to store values to a variable and to retrieve them, take a look at the following simple code examples (Sub or Function wrapper omitted).

Listing 4: Storing to and retrieving from variables

'Store a number to a variable, works the same for integers



MyNumber = 1.234







'Get the contents of one variable and put it in another



CopyOfNumber = MyNumber







'Store a text value, don’t forget the quotation marks



MyName = "Rockman"







'Store a boolean value



IsCorrect = True



IsWrong = False



Here are some more examples, but these are utilizing the suggested variable prefix from Table 1. You can see how that might be useful. I also mixed in a math operator (* for multiplication), more on those later.

Listing 5: More variable examples (with suggested variable prefix)

sngX = 2.5 * 67.586



sngY = sngX * 34.9



blnTrueFalse = True



strName = "Randy Post"



dteBirthday = #7/1/2000# 'If you’re hard coding a date or time, you need



dteLunch = #12:00:00# 'the hash marks (pound sign)



intCount = 2



Another basic aspect of working with variables is appending to them. You would need that for a counter variable, but the concept works for string variables as well. VBA doesn’t have a particularly eloquent way of doing this like some other programming languages, but like I said, I don’t really give a crap about what’s eloquent.

Listing 6: Appending to variables

sngSum = 5.0



sngSum = sngSum + 10.0



'So sngSum now equals 15











'To “append” to strings, also known as concatenation,



'use the ampersand (&) character



strMessage = "The Sum Is: "



strMessage = strMessage & _



sngSum & " units."



Tip: You can break long lines of code with the underscore character preceded by a space. Remember to indent the next line at least one tab for readability (multiple tabs or spaces are fine).

Tip: Another tip, the vbCrLf (carriage return/line feed) character is useful for adding a new line when appending to string variables. Use it like this: strNote = “Line 1” & vbCrLf & “Line 2”



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.