Cara membuat kalkulator dengan macro
Cara membuat kalkulator dengan macro - Langkah mudah Cara membuat kalkulator dengan macro
- Buka Microsoft Office anda
- Save as "enable macro"
- Tekan kombinasi tombol Alt+F11 pada keybord komputer anda
- Inser Userform, cara insert userform liat pada link dibawah ini
- Setelah selesai memasukan userform, klik dua kali untuk menuliskan modul
- Atau langsung saja copy semua modul yang ada pada frame dibawah ini
Option Explicit
Public Collect As Collection
Public CollectBT As Collection
Public CollectTx As Collection
Private Tx As MSForms.TextBox
Private Lb As MSForms.Label
Private Sub UserForm_Initialize()
Dim Bouton As MSForms.CommandButton
Dim Fr As MSForms.Frame
Dim Lig As Integer, Col As Integer, TB, TBc
Dim i As Integer, Cl As iPaOne
Set Collect = New Collection
Set CollectBT = New Collection
Set CollectTx = New Collection
Me.BackColor = &HE0E0E0
Me.Height = 212.25
Me.Width = 206.25
Me.Caption = "Kalkulator iParengan"
Set Tx = Me.Controls.Add("Forms.TextBox.1", "TextBox1", True)
Set Cl = New iPaOne
Set Cl.Texte = Tx
CollectTx.Add Cl
Tx.Move 6, 6, 192, 18
Tx.FontSize = 10
Tx.FontBold = True
Tx.TextAlign = fmTextAlignRight
Set Lb = Me.Controls.Add("Forms.Label.1", "Label1", True)
With Lb
.Move 6, 28, 192, 18
.AutoSize = False
.BackStyle = 1
.BorderColor = &H80000006
.BackColor = &HE0E0E0
.BorderStyle = fmBorderStyleSingle
.FontSize = 10
.FontBold = True
.TextAlign = fmTextAlignCenter
End With
Set Fr = Me.Controls.Add("Forms.frame.1", "Frame1", True)
Fr.Move 6, 54, 114, 132
Fr.BackColor = &HE0E0E0
Fr.BorderStyle = fmBorderStyleSingle
Fr.BorderColor = &H800080
TB = Array(0, 7, 8, 9, 4, 5, 6, 1, 2, 3, 0, 10, 11)
i = 1
For Lig = 0 To 3
For Col = 0 To 2
Set Bouton = Fr.Controls.Add("Forms.CommandButton.1", "Bt" & i, True)
CollectBT.Add Bouton, CStr(TB(i))
Set Cl = New iPaOne
Set Cl.GroupBoutons = Bouton
Collect.Add Cl
With CollectBT(CStr(TB(i)))
.BackColor = &HE0E0E0
.ForeColor = &HFFFFFF
.Tag = TB(i)
.Move 6 + (Col * 36), 6 + (Lig * 30), 31, 26
.FontSize = 14
.FontBold = True
If i < 11 Then
.Caption = TB(i)
ElseIf i = 11 Then
.Caption = "."
Else: .Caption = "="
End If
End With
i = i + 1
Next Col
Next Lig
Set Fr = Me.Controls.Add("Forms.frame.1", "Frame2", True)
Fr.Move 120, 54, 38, 132
Fr.BackColor = &HFFC0FF
Fr.BorderStyle = fmBorderStyleSingle
Fr.BorderColor = &H800080
TB = Array(0, 12, 13, 14, 15)
TBc = Array(" ", "+", "-", "*", "/")
i = 1
For Lig = 0 To 3
Set Bouton = Fr.Controls.Add("Forms.CommandButton.1", "Bt" & TB(i), True)
CollectBT.Add Bouton, CStr(TB(i))
Set Cl = New iPaOne
Set Cl.GroupBoutons = Bouton
Collect.Add Cl
With CollectBT(CStr(TB(i)))
.BackColor = &H404040
.ForeColor = &HFFFFFF
.Tag = TB(i)
.Move 2, 6 + (Lig * 30), 31, 26
.FontSize = 14
.FontBold = True
.Caption = TBc(i)
End With
i = i + 1
Next Lig
Set Fr = Me.Controls.Add("Forms.frame.1", "Frame3", True)
Fr.Move 158, 54, 38, 132
Fr.BackColor = &HFFC0FF
Fr.BorderStyle = fmBorderStyleSingle
Fr.BorderColor = &H800080
TB = Array(0, 16, 17, 18, 19)
TBc = Array(" ", "(", ")", "D", "C")
i = 1
For Lig = 0 To 3
Set Bouton = Fr.Controls.Add("Forms.CommandButton.1", "Bt" & TB(i), True)
CollectBT.Add Bouton, CStr(TB(i))
Set Cl = New iPaOne
Set Cl.GroupBoutons = Bouton
Collect.Add Cl
With CollectBT(CStr(TB(i)))
.BackColor = &H4080&
.ForeColor = &HFFFF&
.Tag = TB(i)
.Move 2, 6 + (Lig * 30), 31, 26
.FontSize = 14
.FontBold = True
.Caption = TBc(i)
End With
i = i + 1
Next Lig
End Sub
Public Sub ControlClick(Index As Integer)
Select Case Index
Case Is < 10: AjouterSurText CStr(Index)
Case Is = 10: AjouterSurText ","
Case Is = 11
On Error GoTo ErreurCalcul
Lb.Caption = Evaluate(Replace(Tx.Text, ",", "."))
Case Is < 18
AjouterSurText CollectBT(CStr(Index)).Caption
Case 18: If Tx.SelLength > 0 Then AjouterSurText ""
Case 19: Tx = "": Lb = ""
End Select
Exit Sub
ErreurCalcul:
MsgBox "Votre calcul comporte une erreur", vbCritical, "Kalkulator iParengan"
End Sub
Sub AjouterSurText(T As String)
If Len(Tx.Text) = Tx.SelStart Then
Tx = Tx & T
Else
Tx = Left(Tx, Tx.SelStart) & T _
& Mid(Tx, Tx.SelStart + 1 + Tx.SelLength)
End If
Tx.SetFocus
End Sub
Public Sub TextDown(ByVal KeyCode As Integer, ByVal Shift As Integer)
If KeyCode = 13 Then
ControlClick 11
End If
End Sub
Public Collect As Collection
Public CollectBT As Collection
Public CollectTx As Collection
Private Tx As MSForms.TextBox
Private Lb As MSForms.Label
Private Sub UserForm_Initialize()
Dim Bouton As MSForms.CommandButton
Dim Fr As MSForms.Frame
Dim Lig As Integer, Col As Integer, TB, TBc
Dim i As Integer, Cl As iPaOne
Set Collect = New Collection
Set CollectBT = New Collection
Set CollectTx = New Collection
Me.BackColor = &HE0E0E0
Me.Height = 212.25
Me.Width = 206.25
Me.Caption = "Kalkulator iParengan"
Set Tx = Me.Controls.Add("Forms.TextBox.1", "TextBox1", True)
Set Cl = New iPaOne
Set Cl.Texte = Tx
CollectTx.Add Cl
Tx.Move 6, 6, 192, 18
Tx.FontSize = 10
Tx.FontBold = True
Tx.TextAlign = fmTextAlignRight
Set Lb = Me.Controls.Add("Forms.Label.1", "Label1", True)
With Lb
.Move 6, 28, 192, 18
.AutoSize = False
.BackStyle = 1
.BorderColor = &H80000006
.BackColor = &HE0E0E0
.BorderStyle = fmBorderStyleSingle
.FontSize = 10
.FontBold = True
.TextAlign = fmTextAlignCenter
End With
Set Fr = Me.Controls.Add("Forms.frame.1", "Frame1", True)
Fr.Move 6, 54, 114, 132
Fr.BackColor = &HE0E0E0
Fr.BorderStyle = fmBorderStyleSingle
Fr.BorderColor = &H800080
TB = Array(0, 7, 8, 9, 4, 5, 6, 1, 2, 3, 0, 10, 11)
i = 1
For Lig = 0 To 3
For Col = 0 To 2
Set Bouton = Fr.Controls.Add("Forms.CommandButton.1", "Bt" & i, True)
CollectBT.Add Bouton, CStr(TB(i))
Set Cl = New iPaOne
Set Cl.GroupBoutons = Bouton
Collect.Add Cl
With CollectBT(CStr(TB(i)))
.BackColor = &HE0E0E0
.ForeColor = &HFFFFFF
.Tag = TB(i)
.Move 6 + (Col * 36), 6 + (Lig * 30), 31, 26
.FontSize = 14
.FontBold = True
If i < 11 Then
.Caption = TB(i)
ElseIf i = 11 Then
.Caption = "."
Else: .Caption = "="
End If
End With
i = i + 1
Next Col
Next Lig
Set Fr = Me.Controls.Add("Forms.frame.1", "Frame2", True)
Fr.Move 120, 54, 38, 132
Fr.BackColor = &HFFC0FF
Fr.BorderStyle = fmBorderStyleSingle
Fr.BorderColor = &H800080
TB = Array(0, 12, 13, 14, 15)
TBc = Array(" ", "+", "-", "*", "/")
i = 1
For Lig = 0 To 3
Set Bouton = Fr.Controls.Add("Forms.CommandButton.1", "Bt" & TB(i), True)
CollectBT.Add Bouton, CStr(TB(i))
Set Cl = New iPaOne
Set Cl.GroupBoutons = Bouton
Collect.Add Cl
With CollectBT(CStr(TB(i)))
.BackColor = &H404040
.ForeColor = &HFFFFFF
.Tag = TB(i)
.Move 2, 6 + (Lig * 30), 31, 26
.FontSize = 14
.FontBold = True
.Caption = TBc(i)
End With
i = i + 1
Next Lig
Set Fr = Me.Controls.Add("Forms.frame.1", "Frame3", True)
Fr.Move 158, 54, 38, 132
Fr.BackColor = &HFFC0FF
Fr.BorderStyle = fmBorderStyleSingle
Fr.BorderColor = &H800080
TB = Array(0, 16, 17, 18, 19)
TBc = Array(" ", "(", ")", "D", "C")
i = 1
For Lig = 0 To 3
Set Bouton = Fr.Controls.Add("Forms.CommandButton.1", "Bt" & TB(i), True)
CollectBT.Add Bouton, CStr(TB(i))
Set Cl = New iPaOne
Set Cl.GroupBoutons = Bouton
Collect.Add Cl
With CollectBT(CStr(TB(i)))
.BackColor = &H4080&
.ForeColor = &HFFFF&
.Tag = TB(i)
.Move 2, 6 + (Lig * 30), 31, 26
.FontSize = 14
.FontBold = True
.Caption = TBc(i)
End With
i = i + 1
Next Lig
End Sub
Public Sub ControlClick(Index As Integer)
Select Case Index
Case Is < 10: AjouterSurText CStr(Index)
Case Is = 10: AjouterSurText ","
Case Is = 11
On Error GoTo ErreurCalcul
Lb.Caption = Evaluate(Replace(Tx.Text, ",", "."))
Case Is < 18
AjouterSurText CollectBT(CStr(Index)).Caption
Case 18: If Tx.SelLength > 0 Then AjouterSurText ""
Case 19: Tx = "": Lb = ""
End Select
Exit Sub
ErreurCalcul:
MsgBox "Votre calcul comporte une erreur", vbCritical, "Kalkulator iParengan"
End Sub
Sub AjouterSurText(T As String)
If Len(Tx.Text) = Tx.SelStart Then
Tx = Tx & T
Else
Tx = Left(Tx, Tx.SelStart) & T _
& Mid(Tx, Tx.SelStart + 1 + Tx.SelLength)
End If
Tx.SetFocus
End Sub
Public Sub TextDown(ByVal KeyCode As Integer, ByVal Shift As Integer)
If KeyCode = 13 Then
ControlClick 11
End If
End Sub
- Belom selesai sekarang anda harus membuat modul
- Pilih >Insert>Class Modul
- Rubah "name"nya menjadi "iPaOne"
- Lalu masukan kode dibawah ini
Option Explicit
Public WithEvents GroupBoutons As MSForms.CommandButton
Public WithEvents Texte As MSForms.TextBox
Private Sub Texte_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Call Kalkulator.TextDown(KeyCode, Shift)
End Sub
Private Sub GroupBoutons_Click()
Call Kalkulator.ControlClick(GroupBoutons.Tag)
End Sub
Public WithEvents GroupBoutons As MSForms.CommandButton
Public WithEvents Texte As MSForms.TextBox
Private Sub Texte_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Call Kalkulator.TextDown(KeyCode, Shift)
End Sub
Private Sub GroupBoutons_Click()
Call Kalkulator.ControlClick(GroupBoutons.Tag)
End Sub
- Tekan F5 untuk mencoba tutorial ini
sumber : http://www.iparengan.com/2014/09/cara-membuat-kalkulator-dengan-macro.html
Tidak ada komentar:
Posting Komentar