Gareth Swarbrick
Page 1/5
Gareth Swarbrick
in the Start Menu -> Program Files ->Microsoft Office submenu. If you cannot locate the icon, or it doesn't open the program, use the file system to locate C:\Program
Files\Office\Excel.exe. Double click to start Excel.
We are now going to record a macro in Visual Basic and edit the result. Type in the numbers 1 to 10 in cells A1 to A10 Select Tools -> Macro -> Record New Macro to start the macro recorder - you should see a small floating tool bar appear Perform the following actions in this sequence:
Tute: Visual Basic Basics Page 2/5
Gareth Swarbrick
Click on cell B1 Type the text =A1^2 Press return to finish editing Select cell B1 and click and drag the small square handle downwards to B10 as shown in Figure 1:
This is called the AutoFill function. A the formula in B1 should replicated through to B10. Select the cells A1:B10 Click on the Chart Wizard tool bar: Select the XY (Scatter) plot option in the Chart Wizard window (see Figure 2) and then press the Finish button
Gareth Swarbrick
Press the Stop Recording button on the floating tool bar: We now have created a macro (usually called Macro1) that can automatically generate a formula series and plot the result. Verify that the macro works by: Delete the chart and
and then press the Run button. Now we are going to edit the Visual Basic code. To do this we need to open the Visual Basic editor: In the Tools -> Macro submenu select the Visual
Basic Editor command (see Figure 3)
Note: The Visual Basic Editor has it's own menu system. Make sure the Project Explorer window is open by activating the menu command (see Figure 4)
In the Project Explorer window click on the small triangle to expand the VBA Project
(Workbook1) and then the Modules folder
Basic code
Page 4/5
Gareth Swarbrick
The project explorer is the main window in which modules are created, opened and deleted. It also shows modules in all other open documents and templates. Your recorded Module should look something like Figure 6. Note the basic components of the Visual Basic subroutine (name, comments, etc.).
Note that the formula in B1 is =RC[-1]^2. Visual Basic has used a relative reference to describe the formula (Same Row, Column[-1]) instead of an absolute reference (A1).Try the following modifications to the program. After you modify the macro select File -> Close and
Return to Microsoft Excel and then re-run Macro1 to see the changes.
Change the formula =RC[-1]^2 to =RC[-1]^0.5 Change the chart type to xlBarClustered Put the ActiveChart.ChartType = xlXYScatter line in a For loop that is executed 10 times (see your notes)
Page 5/5