VBA Exceltutorial1 - 2010
VBA Exceltutorial1 - 2010
Features:
1.
2.
3.
4.
5. Shortcut keys of the macro can also be included in the [Quick Access Toolbar].
Example 1: Develop a macro to calculate the average of five cells to the left of a selected
cell.
Step 0. Initialize a spreadsheet and fill five vertical cells with numbers, similar to Figure 1.
Figure 1.
Step 1. Select the cell to the right ( cell C2 in Figure 1 ).
1
Figure 2.
Step 3. Input formula in cell C2 and then stop macro-recording by selecting
[View]
[Macro]
[Stop Recording]. (see Figure 3)
Figure 3.
Alternatively, you can click on the [Stop] button, usually located at the bottom left
corner of the Excel window (see Figure 4).
Figure 4.
Step 4. Test the macro by filling other five cells with numbers, then select the cell to the right
and then click shortcut-key. ( based on Figure 2, we had chosen [Ctrl-a] )
Figure 6.
Remarks:
1. The single-quote marks the start of comments (characters to the right are ignored).
2. Remove the line with [ Range("C3").Select] if it exists. Otherwise, the macro
will always return to cell C3.
Step 7. Save the macro as a Visual Basic file: select [File]
[Export File..] and save as a
Basic [*.bas] file. ( Note: make sure Module1 is highlighted).
Figure 7.
(Remark: This is one method to access previously-saved macros that will not be affected
too much with the security-handling issues of Excel 2007. Another method is to build
a customized add-in.)
Example 3: Saving and loading macro-enabled worksheets.
This is in case you want to save the spreadsheet together with the macros and functions you
had built for that spreadsheet.
Step 1. Save the worksheet containing the macros by selecting to save it as a macroenabled workbook (see Figure 8.). Close the workbook to test how to load it.
Figure 8.
Step 2. Open the workbook. The [Security Warning] tab should appear on the top part. Click
on [Options].
Figure 9.
Step 3. Select the option that enables the macro.
Figure 10
TIPS:
1. In case you have forgotten the different shortcut keys, you can view, run or edit the
macros you built by selecting [View]
[Macros]
[View Macros], or click [Alt-F8].
2. Another approach is to add the macro into the Quick Access Toolbar. To do so, click on
the customization button (see Figure 7) and select [More Commands].
Figure 7.
Then select the desired macro from the [Macros] list and click [Add>>] button. (See
Figure 10). After clicking [OK], you should notice that addition of the macro button. (see
Figure 11).
Figure 10.
Figure 11.
Example 3: Writing a subroutine to solve a set of linear equations while allowing the
user to input cell-range.
Step 0. Start with a new spreadsheet.
Step 1. Click [Alt-F11] to open the Visual Basic Editor.
[Module]. (see Figure 12)
Step 2. Select [Insert]
Figure 12.
Step 3. Type in the following code:
Figure 13.
Step 3. Go back to the Excel workbook by clicking [Alt-F11] once more. Then click [Alt-F8]
to change the macro options and select shortcut key (plus add description if desired).
Figure 14.
Step 4. Test the macro. First prepare the spreadsheet with a matrix which will be treated as
matrix A and column of cells to be treated as vector b. Then run the macro.
8
Figure 15.
Functions
A function is different from subroutines (or macros) in that they return values.
1. They are bounded by the Function statement and End Function statement.
2. They can take several inputs with different input types
3. Typically, functions are used when iteration loops and decision blocks are needed.
Example of iteration loops:
a) x=0
For i = 1 to 10
x=x+i^2
Next i
b) y=0
Do
i=i+1
y = y+i^2
Loop While y<100
10
Figure 16.
Step 2. Test the function.
=logmean(A1,B1)
=logmean(A2,B2)
Example 4: Writing a function for solving the average of a range of cells.
Function newaverage(x As Range)
n = x.Count
Sum = 0
For i = 1 To n
Sum = Sum + x.Cells(i)
Next i
newaverage = Sum / n
End Function
Example 5: Writing a function that evaluates the real roots of a cubic equation
For the cubic equation:
0
One of the real roots can be found using Newtons method:
Where the iteration is repeated until the difference between
and is below a
specified tolerance.
11
After this first root, , has been found, two real roots will exist if the discriminant is
non-negative, where
2 3 4
If 0, the other two real roots are given by
2
2
A function to implement this is given in Appendix 2 as realCubeRoot(a,b,c,d,n).
12
Effects
[Alt-F8]
View Macro
[Alt-F11]
13
14