Reliable Spreadsheet Solutions - Call Now 281-379-2000 Microsoft Excel - Visual Basic for Applications Consultant Reliable Spreadsheet Solutions - Call Now 281-379-2000 !
PROFESSIONAL SOFTWARE DEVELOPMENT
Reliable Excel VBA Solutions !
 
Microsoft Office : Excel - Visual Basic for Applications [ VBA ] Software 

Beyond Technology

Insight for Microsoft Excel POWER-Users
  Excel Solutions Letter ® FREE  excel solutions letter
Spreadsheet Power-User Tips
  Enable ease and consistency of data entry with a dropdown list of possible entries for a range  dropdown list in cell
  Designing an array formula that functions similar to a VLOOKUP function with multiple criteria and returns a text entry  mult-criteria text lookup
  How Excel workbooks become bloated and what can be done about it  spreadsheet bloat
  Spreadsheet Power-User Tips  more ...
Getting to Know Microsoft Visual Basic for Applications
  Decision Structure that conditionally executes the embedded code  if...then[...else]
  If you are new to VBA and trying to get a handle on programming, this is a MUST READ article for you!  what are variables ?
  Discover a special, more flexible variety of arrays  dynamic arrays
  Getting to Know Microsoft Visual Basic for Applications  more ...
Microsoft Excel Advanced VBA - Just for Geeks
  MUST SEE!  A named range that expands and contracts automatically based on the number of items in the source data table  dynamic named ranges
  MUST SEE! How to identify the last used row in an Excel worksheet  finding real last cell
  Creating charts that adapt as the size of the source data range varies  charting dynamic data
  Microsoft Excel Advanced VBA - Just for Geeks  more ...
Excel Spreadsheet Tools and Tutorials
  Mortgage & Auto Loan Payment Calculator » Amortization with Excel Spreadsheets  amortization guide
  Breakeven Analysis with Excel Spreadsheets Tutorial  breakeven analysis
  Permutations & Combinations with Excel Spreadsheets  combinatorics
  Standard Deviation in Excel Spreadsheets Tutorial  standard deviation
  Weighted Average in Excel Spreadsheets Tutorial  weighted average


Microsoft Excel VBA Custom Application Development
  Microsoft Excel VBA Custom Application Development  excel vba customization
  Microsoft Excel VBA Professional Development Experience  professional experience
  Microsoft Excel VBA Custom Application Development  project request form
Microsoft Excel VBA Solutions : Real-World Project Profiles
  How deep is your data?  engineering analysis
  Financial reporting can be a challenging endeavor.  financial reporting
  To anticipate customers needs is to be rewarded with their business.  sales forecasting
  Are time-hungry paperwork tasks building up on you?  automated billing
  An Engineer-in-a-Box  engineering design
  Partial List of Beyond Technology Clients - Past & Present  clients - past & present


Reliable Spreadsheet Solutions - Call Now 281-379-2000 !
  Visitors' Comments  gratuitous kudos
  Add to Favorites 
  Reliable Spreadsheet Solutions - Call Now 281-379-2000 !  e-mail contact

20831 Rosehill Church Rd
Tomball, Texas 77377


What are Variables?

Rodney POWELL
Microsoft MVP - Excel
need a developer for
your excel project ?
go here
Variables are used to store information temporarily. As a program runs, it holds values temporarily in memory. Variables are names that the program associates with specific locations in memory. The value to which the program refers in these areas can change throughout a session of program operation.

Each variable has a specific type, that indicates how much memory the data requires and the operations that can be performed on that kind of data.

The Dim (stands for dimension) statement is used to declare variables and allocate storage space. It may appear in a General Declarations section at the top of a code module -or- immediately following a procedure declaration. For example ...

Sub SampleCode()

   Dim MyNumber as Integer

  ' Other program statements go here

End Sub

Variables declared within a procedure are not available to other procedures and they only retain values for the life of that procedure. This refers to a variable's scope. Variables declared at a module-level (or within a form's General Declarations section) are available to all procedures within that module or form -and- they continue to retain assigned values for the life of the program. However, these variables are not available to procedures outside of the module in which they are declared. One alternative to the Dim statement is the Public keyword, which expands the scope of the variable to make it available to other procedures outside it's own module or form. It's a better practice to use the narrowest possible scope for your variables.
Note: Other approaches to scope involve Global, Private, and Static declaration. Also, very similar to variables are Constants. You may want to learn about how to use these at some point. Explicit declaration (in contrast to implicit declaration) means requiring the declaration of a variable BEFORE you use it. By default, you are NOT forced to declare a variable, but undeclared variables are problematic. Using the Option Explicit statement forces you to declare every variable. This also helps you catch errors. You should place the Option Explicit statement at the head of the General Declarations section (at the top) in your modules.
Note: You can ensure that the Option Explicit statement will be added automatically by checking Require Variable Declaration in the Editor tab of the Tools | Options dialog of Office's integrated development environment (module) interface. A type clause (i.e., ... as Integer -or- ... as String) or appending a type-declaration character is optional, but it is good practice for this to always be explicitly assigned by you. Valid data types are Byte, Boolean, Integer, Long, Currency, Single, Double, Date, String (variable-length text), String * length (fixed-length text), Object, Variant, a user-defined type, or a specific object type. The compiler will default to the type Variant for a variable that does not have a type specified. A Variant behaves like a chameleon, as it can become whatever type is required for the data assigned to it. This is usually undesirable because it is not memory efficient, it slows performance as VBA has to determine what type of data the Variant represents, and it can result in problematic type conflict errors. Variants DO serve a useful purpose, however, when it is known that the variable type cannot be determined - such as when capturing freeform entry by users.

The following is a summary of basic variable types supported in VBA.

Type Memory Type-Declaration Character Description
Byte 1 byte none Positive whole number ranging from 0 through 255 that can be represented as a binary value.
Boolean 2 bytes none True or False value
Integer 2 bytes % Whole numbers ranging from -32,768 through 32,767.
Long (long integer 4 bytes & Whole numbers ranging from -2,147,483,648 through 2,147,483,647.
Single 4 bytes ! Single-precision floating-point number (with decimal points) ranging from -3.402823E38 to 3.402823E38.
Double 8 bytes # Double-precision floating-point number (which is more precise for very large or very small numbers) ranging from -1.79769313486232E308 to 1.79769313486232E308.
Currency 8 bytes @ Large numbers between -922,337,203,685,477.5808 and 922,337,203,685,477.5807 (15 digits to left of decimal and 4 digits to the right of the decimal).
Date 8 bytes none Represents dates from January 1, 100 through December 31, 9999.
Object 4 bytes none An instance of a class or object reference.
String 10 bytes + 1 byte per char $ Series of any ASCII characters.
String (fixed-length) length of string none Series of any ASCII characters, of a pre-defined length.
Variant min 16 bytes none Any kind of data except fixed-length String data and user-defined types.

When you declare variables, you should choose meaningful variable names that describe the variable's purpose. Variable names must meet the same criteria as procedure names. They can contain a combination of as many as 254 letters, numbers, and the underscore character ( _ ). However, variable names cannot begin with a number, nor can you use reserved keywords that have special meaning to the VBA compiler.

Now, let's see how to apply variables. Refer to the following example:

   Sub VariableDemo()
      Dim Prompt$, varUserInput As Variant


   '  Initialize a String variable with an instruction to
   '  appear in an InputBox

      	Prompt$ = "Please enter something."


   '  Apply your String variable as an argument for the
   '  InputBox function.  Use a Variant variable to capture
   '  the user's entry in an InputBox

      	varUserInput = InputBox(Prompt$)


   '  Display the user's entry by applying your variable
   '  within a message dialog

      	MsgBox varUserInput


   End Sub
A couple of things to notice about this example are how a type-declaration character (that was the $ sign in the Prompt$ variable) can be applied to define the String variable. Also, you can see how a Variant was purposely used since the user could enter either numbers or text in the InputBox.

It would require a lot more to provide exhaustive treatment on the topic of Variables. This serves as a high-level overview of what Variables are about and how to begin applying them properly. For more detailed information, refer to the VBA online Help file or one of the many reference books available on either VBA or Visual Basic programming basics.

Reliable Spreadsheet Solutions - Call Now 281-379-2000 !

Something to add? Let me know.

Beyond Technology Custom Application Development
  home Microsoft Excel VBA Custom Application Development  professional consulting Insight for Microsoft Excel POWER-Users  developer tips Visitors Comments  visitor kudos Beyond Technology - Microsoft Solution Provider  e-mail  

Your suggestions and comments are greatly appreciated. Please keep them coming.

Mail to:   rodney@beyondtechnology.com

All terms, product designs, and company names used in this site may be trademarks or registered trademarks of their respective owners, and are hereby acknowledged. © 1996 - 2008 Beyond Technology. All rights reserved.