VBA for Excel 2007 tutorial-Declaring variables

Declaring variables

In VBA, a variable can be declared at two levels—local and global.

-Declaring a variable at Local Level:
At local level a variable is declared in Sub-procedure  between Sub and End Sub or Function and End Function. By declaring the variable at the local level, the variable scope is limited to only the sub-procedure or function. This means that the  variable can’t be used outside the sub-procedure or function. The variable declaration prototype is:

  Dim variable_name As DataType

   Example: To declare a variable called x to store integer value, you can write as shown below: 

  Sub mylocal
     Dim x As Integer 'Declare a variable called x to store an integer value
  End sub

Note: The sign ' is called comment. Comments can used to explain the code or to exclude the code from being executed.

-Declaring a variable at Global Level: You can declare a variable at the global level by declaring it in the Form module or the Standard module.

+Form module: In the Form module, you will declare a variable in the general section of the form. By declaring the variable in the Form module, the variable can be accessed from any code block of the form but can’t be accessed from other forms.

Example: To declare a variable called lstName to store string value in the Form module, you can write as seen below:

     Dim lstName As String

VBA for Excel: Declaring Variable at global and local levels

+Standard module:
To declare a variable in the Standard module, you need to create a module by using the Visual Basic Editor:

VBA for Excel: Create Module

In the Standard module, you need to declare the variable with public or  private keyword instead of Dim keyword. By declaring with the public keyword the variable can be accessed through out your application. However, with private keyword the variable can be accessed only within its module. The variable declaration in the Standard module can be generally written as shown below:

Public Variable_name As Datatype
Private Variable_name As Datatype
Example: To declare a variable called PI to store a float value in the Standard module, you can write:
Public PI As Single

VBA for Excel: Standard Module Vairable Declaration


seo comment


Hello Web Admin, I noticed that your On-Page SEO is is missing a few factors, for one you do not use all three H tags in your post, also I notice that you are not using bold or italics properly in your SEO optimization. On-Page SEO means more now than ever since the new Google update: Panda. No longer are backlinks and simply pinging or sending out a RSS feed the key to getting Google PageRank or Alexa Rankings, You now NEED On-Page SEO. So what is good On-Page SEO?First your keyword must appear in the title.Then it must appear in the URL.You have to optimize your keyword and make sure that it has a nice keyword density of 3-5% in your article with relevant LSI (Latent Semantic Indexing). Then you should spread all H1,H2,H3 tags in your article.Your Keyword should appear in your first paragraph and in the last sentence of the page. You should have relevant usage of Bold and italics of your keyword.There should be one internal link to a page on your blog and you should have one image with an alt tag that has your keyword....wait there's even more Now what if i told you there was a simple Wordpress plugin that does all the On-Page SEO, and automatically for you? That's right AUTOMATICALLY, just watch this 4minute video for more information at. <a href="http://www.SEORankingLinks.com">Seo Plugin</a>
seo http://www.SEORankingLinks.com/


This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.