Declaring Variables in Excel VBA

In the previous lesson we always used constant values. What if you value change over time? What if that value needs to change with respect to other? Then, this the time you need to use VARIABLE. In our algebra subject, we often encounter X and Y as variables.

You need to store value in the variable and need to declare its Datatype.

How to declare variables:
1. Just use any word you want to use as a variable.
   Tips:
    Don't use 1 letter such as a,b,c,x,y,z etc to declare. This will create confusion when debugging. Try to use a name which can be understood easily.
2. Don't put space.
3. Don't use number only. Must be alphabet or alphanumeric.
4. Some words are already reserved by VBA so you cannot use it anymore.
5. Put the word Dim before you variable and put as Datatype after your variable.
   Dim just let your computer know that the next character is a variable.

What is Datatype?
This is referring to a kind of data you want to store. If I give you the word "June", how will you know that i am referring to a person's name or month? Same is true with your computer. You need to tell what kind of data you are trying to use. Is your data an Integer, string, date and many more?

Integer refers to a whole number.
String refers to a series of character.

Along the way, you will understand more about Datatypes.

Example:

       Sub MyFirstVariable ()
                Dim MyAge as Integer ' Your variable is MyAge. You make this as integer since you cannot put your age in decimal. 
    
       End Sub

In the example above, my age will change over the course of time time. So it will change with respect to the year.

      Sub MyFirstVariable ()

              Dim MyAge as Integer ' Your variable is MyAge. You make this as integer since you cannot put your age in decimal. 

            Dim CurrentYear as Integer '
           Dim MyBirthYear as Integer'

   'Those are the 3 variables i used.

    ' So i will start to make a code on how to get my current age
    ' as we all knew, to calculate my age, just subtract the current year to my birth year

    End Sub

Now let's put a value in your excel sheet.


I will explain to you each line.
 
        CurrentYear = Range ("A2").Value
The value in the right will assign to the left.
In your excel sheet, the value of cell A2 is 2015.
Therefore, Currentyear value now is 2015.

      BirthYear = Range ("B2").Value
The value in the right will assign to the left.
In your excel sheet, the value of cell A2 is 2015.
Therefore, Birthyear value now is 1995.

    MyAge = CurrentYear - MyBirthYear

You can add functions in excel vba as well.
Currentyear minus Mybirthyear equals MyAge.

In this line, MyAge holds the value already.
In order for us to know if it's carrying the answer, we need to show the value in the excel.

That's why in the next line;

   Range("c2").Value = MyAge

Value from right will assign to the left.
MyAge value will now displayed in cell C2

I hope you learn from this tutorial. In the next tutorial, i will be teaching about loops. This is the most interesting part of programming.





No comments:

Post a Comment