terça-feira, 2 de fevereiro de 2010

Entering the code...

The next step is to analyse the code generated by the 'Sample' macro recording. To do that follow this steps:

1.Click ALT+F8 or the Macros button on developer tab.
2.Choose the 'Sample' macro and click edit.
3.You are now on Microsoft Visual Basic editor.You could overpass step 1 and 2 by clicking on ALT+F11 on the worksheet.
4.Now what do we see? We will see somthing similar to this:

Sub Macro1()
'
' Macro1 Macro
'

'
ActiveCell.FormulaR1C1 = "=SUM(RC[1],RC[2],RC[3])"
Range("B1").Select
ActiveCell.FormulaR1C1 = "1"
Range("C1").Select
ActiveCell.FormulaR1C1 = "2"
Range("D1").Select
ActiveCell.FormulaR1C1 = "3"
Range("D2").Select
End Sub

To limit the execution of our routine we use the 'Sub' and 'End Sub' keyword.
Every line that starts with ' is ignored by the code... It is just simply comments and you are free to write whatever you want.

ActiveCell.FormulaR1C1 = "=SUM(RC[1],RC[2],RC[3])"
This line tells that the active cell (the selected cell) is filled with the SUM formula. In our example the A1 cell is filled with the SUM formula.For now, let's just focus on the rest of the code, and then we will see what RC[1] is meant for. If you are really curious you could search on the web by R1C1-style Notation.

Range("B1").Select
Here we select B1 cell,to fill it with the value '1' remeber?

ActiveCell.FormulaR1C1 = "1"
Now we can say that the active cell (B1 cell) is filled with the value 1.

We'll do it for the rest of the cells to fill the values 2 and 3.

Recording macros

Well, the first concept to understand when learning VBA is Macros Development.
What is a macro?
Making it simple,a macro is a list of actions that the user can record to reproduce anytime later.
This concept is global to the whole Office platform, so we can for example develop Macros to write a document on Word, to do some calculations on Excel, or to estimate some times on Project.

How to start digging on Macros?
Well the first step is to enable the developer toolbar. To do that on Office 2007 you go to the Home button, right click on it, and next choose customize quick access toolbar. Then go to Popular tab and check "Show developer tab in the ribbon".
Next you will see a new tab named Developer where you can do the developer stuff.

How to record a macro?
To start recording let's make a first and simple example:
1.Click on Developer tab
2.Click on record macro
3.Name the macro as 'Sample' and click ok.
4.Select A1 cell and write =SUM( and then select B1, C1 and D1 cell and click enter.
5.It should show you 0 because you haven't filled yet the values of B1, C1 and D1.
6.Write 1 on B1, 2 on C1 and 3 on D1.
7.A1 cell should have the value 6 resulted by the sum of the previous cells.
8.Then click on stop recording on the same place you've started recording.
9.Reset your sheet by reseting the fills of each column you've filled before.
10.Select cell A1.
11.Click Macros. A list of all the macros of that worksheet should appear. Select 'Sample' and click run.

Result: All the steps that you've recorded on the macro are reproduced and the sheet is filled again.

In the next example, we will examine the code generated by this recording so don't delete this macro yet.

Here is a pretty cool video more powerfull than our simple example:
Macros

VBA samples for starters

Hi guys!
I was requested to do a small tutorial to introduce some concepts related to VBA and Visual Basic 6 programming so i took the challenge and i'm starting a small series of tutorials related to this technology!
I'm not expert on this topic so you are free to make considerations.