Excecl VBA Classic Excel VBA 2010 Excel VBA 365 Excel VBA Examples About Us

Calculator


This is a typical calculator created using Excel VBA 365. It consists of the number buttons, the operator buttons and some additional command buttons such as the memory button , the clear button and more.

To design the interface, insert 23 command buttons and a label. The label is designated to be the display panel. Next, allocate ten command buttons as the number buttons and change the captions according to their numbers. Besides that, we need to assign four command buttons for the basic operators and one command button for the equal operator. In addition, you need to add the dot command button for decimals. We have also added the M+,M-, MR, C, Sqrt and the MC command buttons.

The design UI is as shown below:

The Calculator UI

The following table list the controls and their corresponding properties:

List of Controls
Control Properties
Cmd_Zero
  • Caption: 0
  • BackStyle:fmBackStyleOpague
  • BackColor:&H8000000F&
  • Font:Tahoma
  • Font Size: 14 Regular
Cmd_one
  • Caption: 1
  • BackStyle:fmBackStyleOpague
  • BackColor:&H8000000F&
  • Font:Tahoma
  • Font Size: 14 Regular
Cmd_two
  • Caption: 2
  • BackStyle:fmBackStyleOpague
  • BackColor:&H8000000F&
  • Font:Tahoma
  • Font Size: 14 Regular
Cmd_three
  • Caption: 3
  • BackStyle:fmBackStyleOpague
  • BackColor:&H8000000F&
  • Font:Tahoma
  • Font Size: 14 Regular
Cmd_Four
  • Caption: 4
  • BackStyle:fmBackStyleOpague
  • BackColor:&H8000000F&
  • Font:Tahoma
  • Font Size: 14 Regular
Cmd_Five
  • Caption: 5
  • BackStyle:fmBackStyleOpague
  • BackColor:&H8000000F&
  • Font:Tahoma
  • Font Size: 14 Regular
Cmd_Six
  • Caption: 6
  • BackStyle:fmBackStyleOpague
  • BackColor:&H8000000F&
  • Font:Tahoma
  • Font Size: 14 Regular
Cmd_Seven
  • Caption: 7
  • BackStyle:fmBackStyleOpague
  • BackColor:&H8000000F&
  • Font:Tahoma
  • Font Size: 14 Regular
Cmd_Eight
  • Caption: 8
  • BackStyle:fmBackStyleOpague
  • BackColor:&H8000000F&
  • Font:Tahoma
  • Font Size: 14 Regular
Cmd_Nine
  • Caption: 9
  • BackStyle:fmBackStyleOpague
  • BackColor:&H8000000F&
  • Font:Tahoma
  • Font Size: 14 Regular
Cmd_Dot
  • Caption: .
  • BackStyle:fmBackStyleOpague
  • BackColor:&H8000000F&
  • Font:Tahoma
  • Font Size: 14 Regular
Cmd_Percent
  • Caption: %
  • BackStyle:fmBackStyleOpague
  • BackColor:&H8000000F&
  • Font:Tahoma
  • Font Size: 14 Regular
Cmd_Plus
  • Caption: +
  • BackStyle:fmBackStyleOpague
  • BackColor:&H8000000F&
  • Font:Tahoma
  • Font Size: 14 Regular
Cmd_Minus
  • Caption: -
  • BackStyle:fmBackStyleOpague
  • BackColor:&H8000000F&
  • Font:Tahoma
  • Font Size: 14 Regular
Cmd_Multiply
  • Caption: x
  • BackStyle:fmBackStyleOpague
  • BackColor:&H8000000F&
  • Font:Tahoma
  • Font Size: 14 Regular
Cmd_Divide
  • Caption: รท
  • BackStyle:fmBackStyleOpague
  • BackColor:&H8000000F&
  • Font:Tahoma
  • Font Size: 14 Regular
Cmd_Equal
  • Caption: =
  • BackStyle:fmBackStyleOpague
  • BackColor:&H8000000F&
  • Font:Tahoma
  • Font Size: 14 Regular
Cmd_Sqrt
  • Caption: Sqrt
  • BackStyle:fmBackStyleOpague
  • BackColor:&H8000000F&
  • Font:Tahoma
  • Font Size: 14 Regular
Cmd_C
  • Caption: C
  • BackStyle:fmBackStyleOpague
  • BackColor:&H8000000F&
  • Font:Tahoma
  • Font Size: 14 Regular
Cmd_MC
  • Caption: MC
  • BackStyle:fmBackStyleOpague
  • BackColor:&H8000000F&
  • Font:Tahoma
  • Font Size: 14 Regular
Cmd_Sqrt
  • Caption: Sqrt
  • BackStyle:fmBackStyleOpague
  • BackColor:&H8000000F&
  • Font:Tahoma
  • Font Size: 14 Regular
Cmd_Plus
  • Caption: M+
  • BackStyle:fmBackStyleOpague
  • BackColor:&H8000000F&
  • Font:Tahoma
  • Font Size: 14 Regular
Cmd_MSubtract
  • Caption: M-
  • BackStyle:fmBackStyleOpague
  • BackColor:&H8000000F&
  • Font:Tahoma
  • Font Size: 14 Regular
Cmd_MR
  • Caption: MR
  • BackStyle:fmBackStyleOpague
  • BackColor:&H8000000F&
  • Font:Tahoma
  • Font Size: 14 Regular
Cmd_MC
  • Caption: MC
  • BackStyle:fmBackStyleOpague
  • BackColor:&H8000000F&
  • Font:Tahoma
  • Font Size: 14 Regular
Lbl_Panel
  • SpecialEffect:fmSpecialEffectSunken
  • BackStyle:fmBackStyleOpague
  • BackColor:&H8000000F&
  • Font:Tahoma
  • Font Size: 14 Regular

The code invloved declaring a number of variables, as shown below:

Dim a, b, c, d, e, f, sqrt, memoplus, memominus As Double

Dim key As Integer
Dim newNum As Boolean
Dim display As Single

The variable newNum is to control the diaplay on the display panel. The following syntax instruct the label panel to clear the display panel if we enter a new number.

If newNum = True Then
Lbl_Panel.Caption = ""

The code for every number button (using command button 1 as an example is as follows:

Private Sub Cmd_One_Click()
If newNum = True Then
Lbl_Panel.Caption = ""

End If
display = Val(Lbl_Panel.Caption + Str(1))
Lbl_Panel.Caption = Str(display)
newNum = False
End Sub

We create a sub procedure for all the operator buttons CheckValue:

Static Sub CheckValue()

displayValue = Val(Lbl_Panel.Caption)
If key = 1 Then
a = displayValue
key = 1
ElseIf key = 2 Then
b = displayValue
key = 2
ElseIf key = 3 Then
c = displayValue
key = 3
ElseIf key = 4 Then
d = displayValue
key = 4
ElseIf key = 5 Then
e = displayValue
key = 5
ElseIf key = 6 Then
memoplus = memoplus + displayValue
key = 6
ElseIf key = 7 Then

memominus = Abs(memominus) - displayValue
key = 7
ElseIf key = 8 Then
sqrt = displayValue
key = 8

End If

newNum = True
End Sub

The code for the equal button is as follows:

Private Sub Cmd_Equal_Click()
If key = 1 Then

f = Val(Lbl_Panel) + a

ElseIf key = 2 Then

f = b - Val(Lbl_Panel)

ElseIf key = 3 Then

f = c * Val(Lbl_Panel)

ElseIf key = 4 Then

f = d / Val(Lbl_Panel)
ElseIf key = 5 Then

f = (e * Val(Lbl_Panel) / 100)

End If

Lbl_Panel.Caption = Str(Round(f, 4))
newNum = True

End Sub

The code for the M+ button is as follows:

Private Sub Cmd_MPlus_Click()
key = 6
CheckValue
End Sub

The code for the M- button is as follows:

Private Sub Cmd_MSubtract_Click()
key = 7
CheckValue
End Sub

The code for the MR button is as follows:

Private Sub Cmd_MR_Click()
If key = 6 Then
f = memoplus

ElseIf key = 7 Then

f = memominus
End If

Lbl_Panel.Caption = Str(Round(f, 4))
newNum = True
End Sub

The code for the C button is as follows:

Private Sub Cmd_C_Click()
Lbl_Panel.Caption = "0"

newNum = True

End Sub

The code for the MC button is as follows:

Private Sub Cmd_MC_Click()
Lbl_Panel.Caption = "0"
displayValue = 0
memoplus = 0
memominus = 0
newNum = True
End Sub

The code for the % button is as follows:

Private Sub Cmd_Percent_Click()
key = 5
CheckValue
End Sub

The code for the Sqrt button is as follows:

f = Sqr(Val(Lbl_Panel.Caption))

Lbl_Panel.Caption = Str(Round(f, 4))
newNum = True

The Runtime UI

The Calculator UI




Copyright ® 2008 Dr.Liew Voon Kiong . All rights reserved   [Privacy Policy]

Contact: Facebook Page