0% found this document useful (0 votes)
9 views13 pages

Lect 7

Uploaded by

ruwaney87
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views13 pages

Lect 7

Uploaded by

ruwaney87
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Customized User Forms (CUF)

 CUF are user defined dialog boxes


(similar to MsgBox and InputBox, but far more flexible)
• CUF can be used to display, enter and modify informations
to the Excel worksheet as well as to the VBA program
• Expl.: most standard windows dialog boxes, such as setup
windows, wizard windows etc.
Creating and designing a CUF:
i) open the user form inside the VB editor
- select Insert UserForm
a new user form object with the default name “UserForm1“
is created
- if the UserForm is not visible:
select View Toolbox 1
The toolbox contains a set of «controls» which you can click on
and drag into the Userform. They include Optionbuttons,
Textboxes, Labels, Listboxes etc.
2
CUFs are employed very often in web-pages through which you
can choose and buy products. For example if you try to buy train
tickets from National Express you will find the following:

These are TextBoxes

These are ComboBoxes

3
These are Option Buttons

This is a Check Box

4
ii) add controls to the user form
- from the toolbox drag and drop the icon of a particular control
to the UserForm
- move and resize the control

- possible options for CUF controls:


Label  A Text added to the CUF to provide general information.
CommandButton  A button that initiates an action.
TextBox  A box in which you can type text.
The text can be linked to a cell on the worksheet.
ListBox  A box that contains a list of items.
The text can be linked to a cell on the worksheet
Combo box  A text box with a drop-down list box.
You can either type or select a choice in the box.
The text can be linked to cells on the worksheet.
5
Check Box  An option you can turn on or off by selecting or
clearing it. More than one check box selected at a time is possible.
Option Button  A button used to select only one of a group of
options.
Frame  A panel in which groups of related controls are organized.
ScrollBar  A control that scrolls through a range of values when
you click the scroll arrows or when you drag the scroll box.
SpinButton  A button that can be attached to a cell or a text box.
Selecting the up arrow increases the value and
selecting the down arrow decreases the value.
MultiPage  A page that allows you to organize controls in form
of several pages.
TabStrip  Displays Tabs you can use to organize other controls.
Toggle button  A button that remains pressed in when selected,
and then releases when it is clicked again.
RefEdit  Enables you to select a range in the worksheet.
Image  A control that embeds a picture into a form. 6
7
iii) once you have added the desired controls to the userform, you
can modify and adjust some of their features by using the
Properties Window.
- depending on the selected control, the Properties Window
contains various types of properties (see examples)
- if the Properties Window is not visible:
select View in the VBA editor Properties Window
or right-click on the corresponding control and select
properties

8
The properties windows for a Combobox and a Textbox looks like this:
iv) check and modify the tab order
- the tab order is the sequence in which the controls receive
the focus
- inside the VBA editor select View TabOrder
a window displaying the controls in a particular order opens
- with “Move up“ or “Move down“ you can change this order

v) associate aVBA-code to the userform


- inside the VBA editor select View Code (or simply
double-click on the form or on a particular control)
- write now the code 10
Expl.:

Create a CUF with title Yes or No and a label saying


“What is your answer?” The form should contain three
OptionButtons with text: “Yes”, “No”, “Don„t know”. When
“Yes“ is selected write “Y” into the cell “D10”, when “No” is
selected , write “N” in the same cell and for “Don‟t know” write
0 in the same cell and open a message box prompting the user to decide!

Once you have created the Userform with all the desired
controls and written the code, you can run it as for any
other subroutine
11
- open a user form
- add a label with text “What is your answer?“
- add three OptionButtons with specified test
- change the caption in the UserForm Properties Window to
“Yes or No“
-view the code of the UserForm (by clicking on the form), it
will say
- Private Sub UserForm_Click()
End Sub

· “private“ means the procedure can only be used in the


current workbook
· “Click“ indicates which event will run this code
(in the Code window there is a ListBox which offers
other possibilities, such as “DblClick“, etc.) 12
- complete the code as follows
Private Sub UserForm_Click()
If OptionButton1.Value=true Then
Range("D10").Value = "Y"
ElseIf OptionButton2.Value=true Then
Range("D10").Value = "N"
ElseIf OptionButton3.Value=true Then
Range("D10").Value = 0
ret=MsgBox(“Decide! Yes or No!”)
End If
End Sub
· OptionButton1, OptionButton2, OptionButton3 are the names of
the OptionButtons. Depending on whether the Option is selected
13
or not they are returned as “True“ or “False“, respectively.

You might also like