Membuat kalkulator Sederhana

Kali ini saya akan buatkan contoh membuat kalkulator sederhana menggunakan Macro VBA Excel.

Untuk lebih jelasnya bisa disimak pada tuturial berikut ini :

Untuk membuat sebuah kalkulator di Excel, tahap pertama buat design kalkulator terlebih dahulu, misalnya saya buat seperti dibawah ini

Untuk script yang digunakan, disimpan pada Clas Module yang diberinama cls_button

Public WithEvents Tombolku As MSForms.CommandButton

Private Sub Tombolku_Click()
UserForm1.Tekan Tombolku.Tag
End Sub

Script pada Userform

Dim ColButton As Collection
Dim clsBtn As Cls_Button

Private Sub UserForm_Initialize()
Set ColButton = New Collection
For Each ctrl In Me.Controls
    If TypeOf ctrl Is MSForms.CommandButton Then
        Set clsBtn = New Cls_Button
        Set clsBtn.Tombolku = ctrl
        ColButton.Add clsBtn
    End If
Next ctrl
End Sub

Public Sub Tekan(tg As Integer)
Select Case tg
Case Is < 10
    Masukan CStr(tg)
Case 10
    Masukan "."
Case 11
    TxtDisplay = Hitung
    TxtTemp = 0
Case Is < 16
    TxtTemp = Hitung
    Masukan Controls("Cmd" & tg).Caption
    TxtDisplay = TxtTemp
    TxtTemp = 0
Case 16
    If Not (TxtTemp = 0 Or TxtTemp = "") Then
        TxtTemp = Left(TxtTemp, Len(TxtTemp) - 1)
    End If
Case 17
    TxtDisplay.Text = ""
    TxtTemp = 0
End Select
End Sub

Private Sub Masukan(txt As String)
If TxtTemp = 0 Then
    TxtTemp = txt
Else
    TxtTemp.Text = TxtTemp.Text & txt
End If
End Sub

Function Hitung()
Hitung = Evaluate(TxtDisplay & TxtTemp)
End Function

Leave a Reply

Your email address will not be published. Required fields are marked *