How to Create a Calculator Using Excel VBA| Full Tutorial

Опубликовано: 03 Октябрь 2024
на канале: The World Of Algorithms
4,204
like

💡 Source Code

Dim fval As String, sval As String, mathsign As Byte, check As Boolean, cntrl As Control
Const calcpath As String = "The direction of the pictures\"

Private Sub UserForm_Activate()

Dim bpic As Byte, rpic As Byte
rpic = WorksheetFunction.RandBetween(1, 4)
With Me
.PictureSizeMode = fmPictureSizeModeStretch
.Picture = LoadPicture(calcpath + "Picture " & rpic & ".jpg")
End With
With CalBackground
For bpic = 1 To 4
.AddItem "Picture " & bpic
Next bpic
End With

End Sub

Sub Btn_Click_Res()
If caltext.Value = 0 Then
caltext.Value = Right(ActiveControl.Name, 1)
Else:
caltext.Value = caltext.Value + Right(ActiveControl.Name, 1)
End If
End Sub

Private Sub btn0_Click()
Call Btn_Click_Res
End Sub

Private Sub btn1_Click()
Call Btn_Click_Res
End Sub

Private Sub btn2_Click()
Call Btn_Click_Res
End Sub

Private Sub btn3_Click()
Call Btn_Click_Res
End Sub

Private Sub btn4_Click()
Call Btn_Click_Res
End Sub

Private Sub btn5_Click()
Call Btn_Click_Res
End Sub

Private Sub btn6_Click()
Call Btn_Click_Res
End Sub

Private Sub btn7_Click()
Call Btn_Click_Res
End Sub

Private Sub btn8_Click()
Call Btn_Click_Res
End Sub

Private Sub btn9_Click()
Call Btn_Click_Res
End Sub

Private Sub CalBackground_Change()
Me.Picture = LoadPicture(calcpath + CalBackground.Text & ".jpg")
End Sub

Private Sub clearsign_Click()
caltext.Value = 0
End Sub

Private Sub divisionsign_Click()
fval = caltext.Value
caltext.Value = caltext.Value + "/"
check = 1
mathsign = 4
End Sub

Private Sub leftsign_Click()
If caltext.Value ◀▶ "" And Len(caltext.Value) ▶ 1 Then caltext.Value = Left(caltext.Value, _
Len(caltext.Value) - 1) Else: caltext.Value = 0
End Sub

Private Sub minussign_Click()
fval = caltext.Value
caltext.Value = caltext.Value + "-"
check = -1
mathsign = 2
End Sub

Private Sub multiplicationsign_Click()
fval = caltext.Value
caltext.Value = caltext.Value + "*"
check = -1
mathsign = 3
End Sub

Private Sub percentsign_Click()
caltext.Value = Val(caltext.Value) / 100
End Sub

Private Sub plussign_Click()
fval = caltext.Value
caltext.Value = caltext.Value + "+"
check = -1
mathsign = 1
End Sub

Private Sub Equalsign_Click()
If check = -1 Then
sval = caltext.Value
If mathsign = 1 Then
sval = Replace(Replace(sval, "+", ""), fval, "")
caltext.Value = Val(fval) + Val(sval)

ElseIf mathsign = 2 Then
sval = Replace(Replace(sval, "-", ""), fval, "")
caltext.Value = Val(fval) - Val(sval)

ElseIf mathsign = 3 Then
sval = Replace(Replace(sval, "*", ""), fval, "")
caltext.Value = Val(fval) * Val(sval)

ElseIf mathsign = 4 Then
sval = Replace(Replace(sval, "/", ""), fval, "")
caltext.Value = Val(fval) / Val(sval)
End If
End If
End Sub

------------------------------------------------------------------------------------------
Buy a Cofee ☕ for theworldofalgorithms: https://ko-fi.com/theworldofalgorithms

🥰Thank you for the donation. I am deeply grateful for your help!🥰
------------------------------------------------------------------------------------------

#excel
#msexcel
#msexceltutorial
#msexcelcourse
#vba
#vbatutorial
#excelvba
#fulltutorial
#excelvbatutorial
#microsoftexceltutorial
#tutorial
#exceltutorial
#advancedexcel
#advancedexceltraining
#tutor
#exceltutor
#exceltutoring
#exceltutoringonline
#onlineexceltutoring
#algorithm
#algorithms
#programming
#program
#script
#vbamacro
#vbamacros
#usingmacro
#veryuseful
#useful
#veryusefulmacro
#freeeducation
#freeprogramming
#freelancer
#excelfreelancer
#vbafreelancer
#freelanceservices
#userform
#vbauserform
#vbacombobox
#combobox
#textboxexcel
#textbox
#vbatextbox
#excelvbatextbox
#calculator
#createacalculator
#calculatorinexcel
#howtocreateacalculator
#simplecalculator
#createasimplecalculator