Microsoft Excel VBA Editor Basic Terms

In this tutorial you will explore the VBA editor and learn the very basic programming.
Don't panic. Programming is not created for Software Engineers or IT. You too can be a programmer. Just start from the basic and you will be expert soon (with constant practice).

1. In the View menu, click again Macro.


2. Click edit. New window will appear.
This is the VBA Editor

Basic Terms
First, let's tackle the terms used in VBA.
1. VBA Project
    This is just a collection of modules.
2. Module
    This is where your macro has been recorded or code to be typed.
    Inside the module you can create multiple subroutines.
3. Subroutine
   This is just a chunk of codes of your macro. This is called subroutine because this can repeat routines whenever you want to.
4. Syntax
    This is just a pattern of your code. Just like in English grammar for example Subject-Verb-Predicate.
    Of course, your computer also needs to understand what you what it to do. You need to be clear with your commands of course.
5. Machine language
    This is the language which your computer will understand. You cannot talk to your computer in your native language. So, each bunch of code you type will be analyze and then converted to 1's and 0's. 1 and 0 are just binary numbers. So don't worry it's not required to learn digital logic.
6. Debug
   It is the process of fixing errors in your code.

Take a look at this code.

Sub MyFirstMacro()
'
' MyFirstMacro Macro
'

'
    Range("A1").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 5287936
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Selection.Font.Bold = True
    ActiveCell.FormulaR1C1 = "ENGINEER"
    Range("A2").Select

End Sub

What is SUB?
As you can see above code, before the name of your macro it has Sub.
Sub just means subroutine. This is the beginning of your routine.
Just like when you create sentence, you need to make your first letter capital and after of each sentence, is a period.
Same is true with vba code (also other programming language they have their own syntax), you need to end your routine by adding END SUB at the end.

What is that apostrophe means?
Well, it;s just a comment. When you have something to write which is not part of your program, you can actually add comment. Always put " ' " when you make comment.
This is very important especially in each line of codes.

Tips:
When coding, make it a habit of adding comments after every line.
Pros:
1.This is for you to understand what you are doing when you open it again.
2. Useful in debugging.
   In my experience, before I never put comments on my codes so when the time comes that I need to debug the code, i need to analyze again from the beginning. This is very troublesome and very time consuming.
3. When you turn over your code to other people, no need for you to recall again the flow. Since it has comment already, then you can easily recall it and explain to her/him well.

Cons:
1. It will also take time to write comment. But if your code is just two lines, i think comment is not necessary.
 I can't think other cons.


Continue to the next tutorial for the syntax explanation and how to debug.


No comments:

Post a Comment