Excel is a popular spreadsheet application from Microsoft. It is all the more powerful for its ability to automate tasks. Tasks, both routine and complex, can be automated into a macro. Macros are essentially shortcuts that trigger an action or sequence of actions. Macros are written in the Visual Basic language. Visual Basic comes bundled with Microsoft Excel.
Macros can be created in two ways.
Private Sub CommandButton1_Click() End SubAdd:
MsgBox ("Hello World")
Visual Basic is a Microsoft programming Language. VB is an event-driven programming language. This means the VB application performs an action based on an user-supplied event, eg. a mouse click or a key press.
When writing a VB application, you need to create a component. The component is usually an object such as a button or a check box. The component is associated with properties and code.
Let's take a closer look at the macro we have created in the previous exercise.
1 Sub CreateHeaders()
2 Range("A1").Select
3 ActiveCell.FormulaR1C1 = "First Name"
4 Range("B1").Select
5 ActiveCell.FormulaR1C1 = "Last Name"
6 Range("C1").Select
7 ActiveCell.FormulaR1C1 = "Exam 1 Grade"
8 Range("D1").Select
9 ActiveCell.FormulaR1C1 = "Exam 2 Grade"
10 Range("E1").Select
11 ActiveCell.FormulaR1C1 = "Final Grade"
12 Range("A1:E1").Select
13 Selection.Font.Bold = True
14 With Select.Interior
15 .ColorIndex = 33
16 .Pattern = xlSolid
17 End With
18 End Sub
Each macro is stored as a subroutine. This subroutine is "CreateHeaders" is displayed in line 1. Lines 2-11 show header creation. In line 3, we see that command to select a range of cells. In this case, the selection is a single cell. Line 4 updates the formula value of the selected cell (ActiveCell).
Line 12 is an example of selecting multiple cells. The following lines show the actions being applied to this range of cells. In line 13, it indicates that the selection should be changed to a bold font. Lines 14-17 show a With structure. The With structure appends the properties in line 15 and 16 to the statement specified in line 14. Lines 14-16 could also be written as:
Select.Interior.ColorIndex = 33 Select.Interior.Pattern = xlSolid
In VB, all subroutines and control loops, except for For/ForEach loops, must be closed. This can be done with a simple end statement, as shown in lines 17 and 18.
VB provides several ways to control programming logic. This allows programs to return output that varies on input.
If ActiveCell.Column = 4 Then
Selection.Font.Bold = true
Else
Selection.Font.Italic = true
End If
For Each mySheet in Worksheets
MsgBox(mySheet)
Next MySheet
For i = 1 To 6
Cells(i,1).Interior.Color = vbYellow
Next i
file = Dir("*.xls")
Do Until file = ""
Cells(row, 1) = file
row = row + 1
file = Dir
Loop
In VB or any programming language, you will sometimes find that you need to store a value temporarily. In VB, you declare variables, with the "Dim" statement. You can set the variable to have a certain property.
Dim newRow As Integer Dim dirFile As String
Functions are quite useful. Instead of assigning a static value to a variable, you can create a function that will generate a value based on the condition that you give it. VB and Excel have quite a few built-in functions. eg. Rnd (VB) or RAND (Excel). It is quite easy to create your own custom function in VB, with the Function stanza. In this new version of Division, we will swap the Divisor w/ the Dividend.
Function newDivision()
Quotient = Dividend/ Divisor
End Function
If you don't have Visual Basic bundled w/ Excel, you can get VB from Microsoft's site.
Macros can be more complex than those shown above. To perform a complex task, divide the task into several simple macros. Then create a parent macro that runs all the smaller macros.
Let's create a macro to calculate simple interest every time you click the button.
Feel free to make as pretty as you wish.
Public Function simpleInterest(Principle As Variant, Rate As Variant, Time As Variant) As Variant
simpleInterest = Principle * (Rate * .01) * (Time / 12)
End Function
Private Sub CommandButton1_Click()
Dim Interest as Variant
Dim p As Variant
Dim r As Variant
Dim t As Variant
p = Range("B1")
r = Range("B2")
t = Range("B3")
Interest = simpleInterest(p, r, t)
Range("B4").Select
ActiveCell.FormulaR1C1 = Interest