(1) Define Your Task:
Before writing macro, you need to decide, what exactly are you going to write. Because Under a single task there will be new task arrived. And VBA executes steps by steps task which is written. So you need to follow, which task you want to write first. Best way is draw a sketch in a hard white paper with pencil and after taking the final decision then start writing in Excel VBA.
(2) Layout of Macro Writing:
Every program has a specific structure. In VBA while we write macros, also need to follow a specific structure. But this is a sample structure. 1 thing you should memorized that, Procedure starts with a function declaration. Then inner task commands, some comments for taken note if in future require editing, and finally closing the procedure. Some thing like this flow chart:
(3) Writing Macros pretest:
First we will look what we wish to do in a simple English language, and how VBA writes it, in VBA language.
Write in Simple language:
1. Create a function February()
2. Click Sheet2
3. Click cell B1
4. Type from keyboard February
5. Select the Cell B2
6. Type 200
7. End the program
Now Write in VBA Language:
Now it's time to write your first simple macro:
(a) Press Alt+F11 to go to the VBA Environment.
(b) Click on Insert | Modules to take a new module.
(c) Double Click on the module and write the below VBA code, in Code Window:
(e) Press F5 to run the macro
(f) A message box will show as below:
(g) Click on OK.
(h) Save the file as .xlsm to enable macro and close it for today.
Before writing macro, you need to decide, what exactly are you going to write. Because Under a single task there will be new task arrived. And VBA executes steps by steps task which is written. So you need to follow, which task you want to write first. Best way is draw a sketch in a hard white paper with pencil and after taking the final decision then start writing in Excel VBA.
(2) Layout of Macro Writing:
Every program has a specific structure. In VBA while we write macros, also need to follow a specific structure. But this is a sample structure. 1 thing you should memorized that, Procedure starts with a function declaration. Then inner task commands, some comments for taken note if in future require editing, and finally closing the procedure. Some thing like this flow chart:
Image 1: VBA Programming simple diagram
(3) Writing Macros pretest:
First we will look what we wish to do in a simple English language, and how VBA writes it, in VBA language.
Write in Simple language:
1. Create a function February()
2. Click Sheet2
3. Click cell B1
4. Type from keyboard February
5. Select the Cell B2
6. Type 200
7. End the program
Now Write in VBA Language:
Sub February() Worksheets("Sheet1").Select Range("B1").Select ActiveCell.Value = "February" Range("B2").Select ActiveCell.Value = 200 End Sub
Now it's time to write your first simple macro:
(a) Press Alt+F11 to go to the VBA Environment.
(b) Click on Insert | Modules to take a new module.
(c) Double Click on the module and write the below VBA code, in Code Window:
Image 2: Writing macro in module
(e) Press F5 to run the macro
(f) A message box will show as below:
Image 3: First VBA program
(g) Click on OK.
(h) Save the file as .xlsm to enable macro and close it for today.