Professional Documents
Culture Documents
I have also included Form to automate the database (Automate Survey) in excel sheet from
STEP 6 onward.
Select UserForm from Insert menu. That will open a User form window like above
The Controls for a UserForm can be found on what is known as the "Toolbox". When we Insert
a UserForm from within the VBE (Visual Basic Environment) via Insert > UserForm the Toolbox
will be displayed by default. Or we can go to View>Toolbox. The Toolbox contains a single page
tab aptly called "Controls". It is here that we will see all the Visual Basic controls plus any
ActiveX Controls that may have been added. By default there will be sixteen Controls available
these are:
1.Label
2.TextBox
3.ComboBox
4.ListBox
5.CheckBox
6.OptionButton
7.ToggleButton
8.Frame
9.CommandButton
10.TabStrip
11.MultiPage
12.ScrollBar
13.SpinButton
14.Image
15.Select Objects
16RefEdit
Drag and drop the Command Button in the useform.Use command Button for each operators.
This can be done in a simple way by just copy a button by Ctrl+C and pasting by Ctrl+V.
Then change the size of button to make then somewhat look like a calculator.
Just right click on button >Properties > Caption .Change the caption of each button.
Also add TextBox tool to userform ,which can be used to type the operands
We can add a Background image by selecting an image file usually jpeg files from a file.
i.e Select Userform then right click on the form >Properties> image then select a picture
My code is given below , please note that the Command Button caption will be different.
Please see that caption for each command button. Double click on the commandButton and
type code for each of the commands
'Addition operator
End Sub
Ans.Text = -Ans.Text
End Sub
Private Sub CommandButton31_Click()
If (Val(Op2.Text) = 0) Then
'Division operator
Exit Sub
Else
End If
End Sub
Private Sub
CommandButton32_Click()
'Multiplication operator
End Sub
'Minus operator
End Sub
Ans.Text = Ans.Text ^ (1 / 2)
End Sub
'1/x operator
If (Val(Ans.Text) = 0) Then
Ans.Text = "Division by Zero"
Exit Sub
Else
Ans.Text = 1 / (Ans.Text)
End If
End Sub
Ans.Text = 0
End Sub
End Sub
In order to automatically open the UserForm when excell is opened type the following code in
the ThisWorkbook
UserForm1.Show
End Sub
Mycalculator.xlsx
Step 5: Make Your Own Calculator
You can add many features to this and make your own calculator.
We can do it in Visual studio in same way and can create .exe files.
1. Open the Visual Basic Editor. If the Project Explorer is not visible, click View, Project Explorer.
2. Click Insert, Userform. If the Toolbox does not appear automatically, click View, Toolbox.
Your screen should be set up as below.
3. Add the controls list as in the picture . Once this has been completed, the result should be
consistent with the picture of the Userform shown earlier. For example, create a text box
control by clicking on TextBox from the Toolbox. Next, you can drag a text box on the Userform.
When you arrive at the Car frame, remember to draw this frame first before you place the two
option buttons in it.
4. Change the names and captions of the controls according to the table below. Names are used
in the Excel VBA code. Captions are those that appear on your screen. It is good practice to
change the names of controls. This will make your code easier to read. To change the names
and captions of the controls, click View, Properties Window and click on each control.
Family_Form.Show
End Sub
We are now going to create the Sub UserForm_Initialize. When you use the Show method for
the Userform, this sub will automatically be executed.
2. In the Project Explorer, right click on Family_Form and then click View Code.
3. Choose Userform from the left drop-down list. Choose Initialize from the right drop-down
list.
'Empty NameTextBox
NameTextBox.Value = ""
'Empty PhoneTextBox
PhoneTextBox.Value = ""
'Fill CityListBox
With CityListBox
.AddItem "Kochi"
.AddItem "Mumbai"
.AddItem "Delhi"
.AddItem "Agartala"
.AddItem "Agra"
.AddItem "Agumbe"
.AddItem "Ahmedabad"
.AddItem "Aizawl"
End With
'Empty StatusComboBox
StatusComboBox.Clear
'Fill StatusComboBox
With StatusComboBox
End With
'Uncheck DataCheckBoxes
CarOptionButton2.Value = True
'Empty Members
Members.Value = ""
TextBox NameTextBox.SetFocus
End Sub
MoneyTextBox.Text = MoneySpinButton.Value
End Sub Explanation: this code line updates the text box when you use the spin button.
Private Sub
MemberButton_Change()
Members.Text = MemberButton.Value
End Sub
'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
'Transfer information
Else
End Sub
Explanation: first, we activate Sheet1. Next, we determine emptyRow. The variable emptyRow
is the first empty row and increases every time a record is added. Finally, we transfer the
information from the Userform to the specific columns of emptyRow.
Call UserForm_Initialize
End Sub
Explanation: this code line calls the Sub UserForm_Initialize when you click on the Clear button.
Unload Me
End Sub
Explanation: this code line closes the Userform when you click on the Cancel button.
Form.xls