Controls

advertisement
Excel VBA - City University Continuing Education
VBA for Microsoft Excel – Part 9
Please note the interface Excel 2007 is radically different to that of previous
versions. However I do not expect much, if any, difference within the
programming code.
Course web site:
3.
in the window that appears ensure that Popular is highlighted
4.
put a tick in the checkbox that says Put the Developer Tag in the Ribbon
http://www.staff.city.ac.uk/s.walsh/vba
Table of Contents
1
2
The Developer tab .......................................................................................... 1
Creating a command button ......................................................................... 2
2.1
Code associated with the button ........................................................ 3
3
Spin control ..................................................................................................... 3
4
Combo box ...................................................................................................... 4
4.1
Creating code for the Combo box ...................................................... 4
5
A Scroll bar to change the interest rate ....................................................... 4
1
The Developer tab
You can add custom controls to a worksheet – the main advantage is that you
can make your sheet easier to use by giving it the feel of a finished package
In order to get started you must have the Developer tab on your Ribbon, by
following these steps:
1.
click the icon to display the Office toolbar
2.
choose Excel options at the bottom of the display
The Developer tab displays the options to run or record a macro, and to switch
to the VB Editor.
If you use the Insert button you will display the available tools, divided into
two categories: Form controls and Active X controls. While they look identical,
they work in a different way. Use the Active X controls if you can, as they are
more up-to-date.
Page 1 of 4
Excel VBA - City University Continuing Education
When you create the Command button, the Design mode button is automatically
turned on. In Design mode you can customise the button by setting its properties,
and attach code to the button.
Click the Properties button to display the Properties window.
If you have used controls in previous versions of Excel, the Form controls
correspond to the Forms toolbar, and the Active X controls correspond to the
Control toolbox.
2
Creating a command button
To place a command button on your worksheet, click the command button tool
then draw a rectangle on your worksheet to the size that you want your button.
A selection of relevant properties is as follows:
• Name – the name of the button to be referred to in VBA code
• Caption – the text as displayed on the button itself – only for the user – not to
be used in VBA code to refer to the object
• Take Focus on Click – when you click the button, if this Property is false, the
currently selected cell or object remains selected
• By changing the caption, you can see that the button display changes. Note
that its name has been changed to cmdClose - spaces are not allowed in names,
but are allowed in captions.
Close the Properties window when you have finished with it.
Page 2 of 4
Excel VBA - City University Continuing Education
2.1
Code associated with the button
3
Spin control
At the moment if you click the button nothing happens because no functionality You can use a spin control to input data to a cell. In the following example, the
is attached to it.
cells contain arguments supplied to the PMT function which works out
payments on a loan.
To create code, in Design mode double-click the button. You will see the
beginning and end of a procedure. This code is an event procedure, specifically
for the click event of the button. The first line has the optional keyword Private this means the code can only run when the button is clicked, it is not available
to any other mechanism.
The name of the sub consists of:
the name of the button
an underscore
the event, i.e. Click
and the brackets
All event handling code follows this pattern.
You can of course change the values manually, but it could more convenient to
have a control to do it.
We will write a simple program to close the workbook.
Click the Spin control tool from the Active X controls and draw a small
rectangle adjacent to cell D7
Private Sub cmdClose_Click()
Display the properties of the Spin control – the important options are:
Dim response As Integer
• Max value 10
response = MsgBox("Are you sure you want to close?" _
, vbYesNo, "City University")
• Min value 1
If response = vbYes Then
ThisWorkbook.Close
Else
MsgBox "You clicked No"
End If
End Sub
• Linked cell D7
• Small change value 1
Linked cell is the crucial property, it is the cell that gets its value from the
control.
The code is located in the sheet module for the sheet where the button was
placed.
Page 3 of 4
Excel VBA - City University Continuing Education
4
Combo box
Let’s say you have a list of values – for example, H3:I9. You might want to tie
the price to one of the values in the list. This can be done with a Combo box.
To make life easier we will create a named range called CarList for the data in
H3:I9.
4.1
Creating code for the Combo box
You can protect linked cells – so they can only be edited using the combo box or
spin button. While in Design mode, double click the Control. This takes you to
the sheet module.
Enter the following code for the combo box’s Change event
Private Sub cboPrice_Change()
Range("D3").Value = CboPrice.Value
End Sub
On returning to the sheet you should find that if you exit Design mode,
selecting a value from the combo box links directly to cell D3.
Insert a combo box from the Active X controls. Name the combo box cboPrice.
5
A Scroll bar to change the interest rate
It would be very convenient to have a control that could change the interest rate
in the function. A scroll bar can do this, though setting it up is a little tricky.
Click the scroll bar tool from the Active X controls, and draw the scroll bar in
row 6, adjacent to the interest rate.
Here are the other properties to change:
• Style 2 - fmStyleDropDownList
• Listfillrange CarList (define a name if necessary). Also try the range itself:
H3:I9
• columnCount 2
• BoundColumn 2 (to access prices, not car name)
Scroll bars work much like Spin buttons, but you can take larger steps. By its
nature, you may want to change the interest rate by fractions of one percent. To
get round the problem of only being able to increment its value by 1 (which is
100%), we need to resort to a bit of guile. With the scroll bar set the properties as
follows:

Max property to be 2000

Small Change property to be 5

Large Change property to be 100

Linked Cell property to be somewhere else, say H1 (you can hide it later)
– the value from the scroll bar will be displayed here.
Now change the formula in D6 to be =H1/10000.
Page 4 of 4
Download