Excel VBA - City University Continuing Education
1 Developer Tab in Excel 2010 /2013 ............................................................. 1
2 Creating a command button ......................................................................... 1
3 Spin control ..................................................................................................... 2
4 Combo box ...................................................................................................... 2
4.1
Creating code for the Combo box ...................................................... 3
5 A Scroll Bar to change the interest rate ....................................................... 3
6 Toggle Button ................................................................................................. 3
7 List Box ............................................................................................................ 4
7.1
MultiSelect property of a Listbox ........................................................ 4
To display the Developer tab on the Ribbon, follow these steps:
1.
click the File menu in Excel
2.
choose Options (second from the bottom)
3.
in the list on the left click Customize Ribbon
4.
in the List Box on the right check the entry for Developer
5.
Click OK
To place a command button on your worksheet, select the
Developer ribbon, click the Insert button. This displays two sets of buttons, Form
Controls and
ActiveX
controls.
Note that though they look the same, you use them in entirely different ways.
ActiveX
controls have superseded
Form
controls, and the differences is that you can write code for the
ActiveX
controls, whereas with
Form controls you can only atttach a macro.
Click the command button tool from the ActiveX controls, then draw a rectangle on your worksheet to the size that you want your button.
Page 1 of 4
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. If necessary, click the Properties button and change the
Name
of the button and its
Caption
. Close the
Properties
window when you have finished with it.
A quick way to create code, while still in Design mode, is to double-click the button. This creates the beginning and end of a procedure, located in the sheet module for the sheet where the button was placed.
You can use a Sp in control to input data to a cell. In the following example, the cells contain arguments supplied to the
PMT
function which works out payments on a loan.
Excel VBA - City University Continuing Education
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.
T o make life easier we will create a named range called CarList for the data in
H3:I9
.
You can of course change the values manually, but it is easier to have a control to do it. Click the
Spin
control tool from the Active X controls and draw a small rectangle adjacent to cell D7
Display the properties of the Spin control – the most important ones are:
• Max value 10
• Min value 1
• Linked cell D7; the Linked cell is the cell that gets its value from the control
• Small change value 1
Click the combo box icon from the Active X controls, and draw it to the required dimensions on the worksheet (it only needs to be about one row in height). Display properties and change the Name property of the combo box cboPrice .
Here are the other properties to change:
• Style choose 2 – fmStyleDropDownList ( this means that the user can only pick an item from the list, and cannot type anything into the combo box )
• ListFillRange type
CarList
• ColumnCount set it to 2
• BoundColumn set it to 2 (this means the value returned by the combo box will be from the second column, i.e. the price and not the description)
Page 2 of 4
Y ou can finalise the column widths after you are satisfied that the control functions correctly. Click in the
ColumnWidths
property and type say,
3.5 cm; 2cm the first number refers to the first column width, and so on – the semicolon is the separator. You should include the cm unit, if you just type a number it defaults to
Points
.
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.
It would be very convenient to have a control that could change the interest rate in the function. A scroll b a r 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.
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
Excel VBA - City University Continuing Education
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
.
A
Toggle
button looks superficially like a
Command
button, but by default it has two states, which means that you can write some code for the first click, and some other code for the second click. Invariably an
If Then Else structure is used; commands for the first click go in the Then clause, commands for the second click go in the Else clause. When the button is clicked for the first time it returns
True
, and that is sufficient to be able to write the code.
A toggle button is suitable for simple operations, for example, to show or hide some data. The following code hides Column
H
on the first click and shows Column H on the second click (you could change the code to show/hide rows if you wish)
Private Sub tglHide_Click()
If tglHide = True Then
tglHide.Caption = "Show"
Columns("H").Hidden = True
Else
tglHide.Caption = "Hide"
Columns("H").Hidden = False
End If
End Sub
Note that one would usually write code to change the caption for each of the clicks .
Page 3 of 4
A Listbox is similar to a Combo box in that you can use its Value property to refer to the selected item. It differs visually in that it can display multiple values.
You can use its ListFillRange property to populate it with values from the same worksheet, eg
H12:H16
It’s default event is the Click event, but this is too volatile for many purposes, so it’s typical to use the
Click
event of a button to refer to the selected item in the List box.
For example assuming the Listbox in the illustration is named Listbox1 then the Command button code could be
Private Sub CommandButton1_Click()
MsgBox "The selected value is " & ListBox1.Value
End Sub
The Value property of the ListBox is the value of the selected item.
The property that makes the
ListBox
more versatile is
MultiSelect
, which means the user can select several items rather than just one. There are three possibilities for the MultiSelect property:
Excel VBA - City University Continuing Education fmMultiSelectSingle
(default)
Only one item can be selected fmMultiSelectMulti
(simple) Clicking selects or deselects an item in the list. fmMultiSelectExtended Pressing
SHIFT
and clicking the mouse, extends the selection from the previously selected item to the current item. Pressing
CTRL
and clicking the mouse selects or deselects an item.
When the
MultiSelect
property is set to
Simple
or
Extended
you can’t use the
Value
property, but you must use the
List
property with an integer number to reference the item. Furthermore, the numbering is zero-based, which means it starts at zero, so
List
(
0)
references the first item,
List(1)
references the second item and so on, So, the expression
ListBox2.List(4)
references the fifth item of ListBox2 .
In order to exploit the
MultiSelect
property, you must use a loop. The following code demonstrates the method.
Private Sub cmdDemo2_Click()
Dim i As Integer
For i = 0 To ListBox2.ListCount - 1
If ListBox2.Selected(i) Then
MsgBox "We fly to " & ListBox2.List(i)
ListBox2.Selected(i) = False
End If
Next i
End Sub
The
Selected
property of the List box
is used to determine whether or not a particular item is selected. Unfortunately there is no simple Clear or
Refresh
method for the listbox, so the
Selected
property is set to
False
in the loop.
Page 4 of 4