Вы находитесь на странице: 1из 5

Advanced Computing Skills

Gareth Swarbrick

Tute: Visual Basic Basics

Page 1/5

Advanced Computing Skills

Gareth Swarbrick

Tute: Visual Basic Basics


For this tutorial you will be using Visual Basic in Microsoft Excel. All versions of Excel from Excel 6 onwards contain the ability to create Visual Basic subroutines and functions. You will need to use a Windows NT workstation for this tutorial. These are available on the 6th floor (CE611) and the 2nd floor (CE201). On an NT workstation look for the Excel icon: in the Program Manager, or

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.

Recording and Editing a Macro


After Excel starts up you should see an Excel worksheet which looks something like this:

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

Advanced Computing Skills

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:

Figure 1: Using the AutoFill function

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

Figure 2: Selecting the XY (Scatter) plot option

Position the chart if required.


Tute: Visual Basic Basics Page 3/5

Advanced Computing Skills

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

formulas in Column B Run the macro by selecting


Tools -> Macro -> 1 Figure 3: Recording a new macro in Excel Macros, select Macro

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)

Figure 4: Project Explorer

In the Project Explorer window click on the small triangle to expand the VBA Project
(Workbook1) and then the Modules folder

(see Figure 5) Double click on Module1 to view the Visual


Figure 5: Opening a module
Tute: Visual Basic Basics

Basic code
Page 4/5

Advanced Computing Skills

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.).

Figure 6: Visual Basic code for a recorded macro

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)

Tute: Visual Basic Basics

Page 5/5

Вам также может понравиться