KEY steps) :
To createc calculator, you can see the design of the userform in the section. Let me explain the
key steps to develop this caculator project. We are going to write multiple procedure s for
multiple controls with the below approach.
Step 1: Create UserForm: we have to first create the userform from the Insert menu in
VBA Editor.
Step 2: Place toolbox controls on the created userform: Place required controls on
the userform. You can drag the controls from the toolbox and drop controls on the
userform.
Step 3: Set properties and alignment of all controls: Properties of each control has
set. It is shown in the design section.
Step 4: Clear Button: It is used to clear the display area.
Step 5: Back Button : It is used to go back one digit.
Step 6: Divide (/) Button: It is used for division.
Step 7: Multiplcation (*) Button: It is used for multiplication.
Step 8: Minus (-) Button: It is used for subtraction.
Step 9: Add (+) Button: It is used for addition.
Step 10: Dot (.) Button : It is used to add dot to the number.
Step 11: One (1) Button: It is used to display number 1.
Step 12: Two (2) Button: It is used to display number 2.
Step 13: Three (3) Button: It is used to display number 3.
Step 14: Four (4) Button: It is used to display number 4.
Step 15: Five (5) Button: It is used to display number 5.
Step 16: six (6) Button: It is used to display number 6.
Step 17: Seven (7) Button: It is used to display number 7.
Step 18: Eight (8) Button: It is used to display number 8.
Step 19: Nine (9) Button: It is used to display number 9.
Step 20: Zero (0) Button: It is used to display number .0
Step 21: EqualTo (=) Button: It is used to perform all calculations like addition,
substraction, multiplcation, etc .
Step 22: Textbox: It is used to display result.
Enabled False
SpecialEffect 0-frmSpecialEffectFlat
TextAlign 3-frmTextAlignRight
Value 0
Enabled False
SpecialEffect 0-frmSpecialEffectFlat
TextAlign 3-frmTextAlignRight
EnterKeyBehavior True
Caption Clear
Caption Back
Caption /
Caption *
Caption –
Control Property Value
Caption +
Caption .
Caption =
Caption 1
Caption 2
Caption 3
Caption 4
Caption 5
Caption 6
Caption 7
Control Property Value
Caption 8
Caption 9
Caption 0
This is what I used to create calculator design. By changing or setting all the above properties
and values of control the form will be looking like below.
In the above code we are setting maximum lenth of display numbers is 10. You can
change this number.
In the above code we are displaying message when displaying numbers are exceeding
10.
Step 6: Double click on the ‘ Divide (/) Button’ and add the
below code.
Step 8: Double click on the ‘ Minus (-) Button’ and add the
below code.
Step 9: Double click on the ‘ Add (+) Button’ and add the
below code.
Step 10: Double click on the ‘ Dot (.) Button ‘ and add the
below code.
Step 11: Double click on the ‘ One (1) Button’ and add the
below code.
Private Sub cmdBtn1_Click()
If txtRes = 0 Then
txtRes = cmdBtn1.Caption
Else
txtRes = txtRes + cmdBtn1.Caption
End If
End Sub
Step 12: Double click on the ‘ Two (2) Button’ and add the
below code.
Step 13: Double click on the ‘ Three (3) Button’ and add
the below code.
Step 14: Double click on the ‘ Four (4) Button’ and add the
below code.
Step 15: Double click on the ‘ Five (5) Button’ and add the
below code.
Step 16: Double click on the ‘ six (6) Button’ and add the
below code.
Step 17: Double click on the ‘ Seven (7) Button’ and add
the below code.
Step 19: Double click on the ‘ Nine (9) Button’ and add the
below code.
Step 20: Double click on the ‘ Zero (0) Button’ and add the
below code.
Step 21: Double click on the ‘ EqualTo (=) Button’ and add
the below code.
End Select
txtdisplay = Empty
End If
ErrOcccered:
End Sub
‘Variable Declaration
Public tmpVar As String
Public calVal As String
'2. To displaying message when exceeding more then 10 numbers on the To display area.
Private Sub txtRes_Change()
If txtRes.TextLength > 10 Then
MsgBox "Its Too long to calculate value.", vbInformation
txtRes.Text = Left(txtRes.Text, 10)
Exit Sub
End If
End Sub
End Sub