Friday, August 17, 2018

Create Calculator Using Microsoft Word VBA

This video tutorial describe how to create Calculator using Microsoft Word VBA. This is simple and easy steps to create calculator helps students to interact with VB Environment. For complete guide watch the video below.




Complete code:
-----------------------------------------------------------
Option Explicit
Dim num1 As Double
Dim num2 As Double
Dim op As String
Dim result As Double

-----------------------------------------------------------

Private Sub CommandButton1_Click()                       // For button 7
If TextBox1.Text = "0" Then
TextBox1.Text = "7"
Else
TextBox1.Text = TextBox1.Text & "7"
End If
End Sub
-----------------------------------------------------------

Private Sub CommandButton10_Click()                  // For button 3
If TextBox1.Text = "0" Then
TextBox1.Text = "3"
Else
TextBox1.Text = TextBox1.Text & "3"
End If
End Sub
-------------------------------------------------------------
Private Sub CommandButton11_Click()                    // For button 2
If TextBox1.Text = "0" Then
TextBox1.Text = "2"
Else
TextBox1.Text = TextBox1.Text & "2"
End If
End Sub
--------------------------------------------------------------  
Private Sub CommandButton12_Click()                    // For button 1
If TextBox1.Text = "0" Then
TextBox1.Text = "1"
Else
TextBox1.Text = TextBox1.Text & "1"
End If
End Sub
---------------------------------------------------------------
Private Sub CommandButton13_Click()                      // For operator /
num1 = TextBox1.Text
TextBox1.Text = 0
op = "/"
End Sub
---------------------------------------------------------------
Private Sub CommandButton14_Click()                   // For operator =
num2 = TextBox1.Text
If op = "+" Then
result = num1 + num2
TextBox1.Text = result
ElseIf op = "-" Then
result = num1 - num2
TextBox1.Text = result
ElseIf op = "x" Then
result = num1 * num2
TextBox1.Text = result
ElseIf op = "/" Then
result = num1 / num2
TextBox1.Text = result
End If
End Sub
-------------------------------------------------------------------
Private Sub CommandButton15_Click()                         // For button .
If TextBox1.Text = "0" Then
TextBox1.Text = "."
Else
TextBox1.Text = TextBox1.Text & "."
End If
End Sub
--------------------------------------------------------------------------
Private Sub CommandButton16_Click()                                     // For button 0
If TextBox1.Text = "0" Then
TextBox1.Text = "0"
Else
TextBox1.Text = TextBox1.Text & "0"
End If
End Sub
--------------------------------------------------------------------------------
Private Sub CommandButton17_Click()                                          // For button Clear
TextBox1.Text = 0
End Sub
----------------------------------------------------------------------------
Private Sub CommandButton2_Click()                                             // For button 8
If TextBox1.Text = "0" Then
TextBox1.Text = "8"
Else
TextBox1.Text = TextBox1.Text & "8"
End If
End Sub
-------------------------------------------------------------------------------
Private Sub CommandButton3_Click()                                            // For button 9
If TextBox1.Text = "0" Then
TextBox1.Text = "9"
Else
TextBox1.Text = TextBox1.Text & "9"
End If
End Sub
---------------------------------------------------------------------------------
Private Sub CommandButton4_Click()                                           // For button +
num1 = TextBox1.Text
TextBox1.Text = 0
op = "+"
End Sub
----------------------------------------------------------------------------------
Private Sub CommandButton5_Click()                                         // For button =
num1 = TextBox1.Text
TextBox1.Text = 0
op = "-"
End Sub
------------------------------------------------------------------------------------
Private Sub CommandButton6_Click()                                                   // For button 6
If TextBox1.Text = "0" Then
TextBox1.Text = "6"
Else
TextBox1.Text = TextBox1.Text & "6"
End If
End Sub
-------------------------------------------------------------------------------------
Private Sub CommandButton7_Click()                                                    // For button 5
If TextBox1.Text = "0" Then
TextBox1.Text = "5"
Else
TextBox1.Text = TextBox1.Text & "5"
End If
End Sub
--------------------------------------------------------------------------------------
Private Sub CommandButton8_Click()                                                 // For button 4
If TextBox1.Text = "0" Then
TextBox1.Text = "4"
Else
TextBox1.Text = TextBox1.Text & "4"
End If
End Sub
---------------------------------------------------------------------------------------
Private Sub CommandButton9_Click()                                                    // For button x
num1 = TextBox1.Text
TextBox1.Text = 0
op = "x"
End Sub

No comments:

Post a Comment