Macro adalah Sebuah fasilitas yang dimiliki Microsoft Excel yang dapat digunakan untuk merekam semua tindakan dan perintah yang dilakukan pada program Excel. Alat bantu yang digunakan dalam pembuatan perintah maupun perekaman Macro adalah dengan menggunakan Visual Basic Editor.
Berikut langkah-langkahnya:
1. Buka Excel lalu tekan kombinasi tombol ALT+F11 pada keyboard. VBA editor telah aktif dan akan muncul tampilan seperti ini:
Setiap setelah membuat Label, jangan lupa untuk mengubah caption sesuai dengan data yang dibutuhkan.
Gunakan Combo Box pada ToolBox sebagai syarat untuk pengisian data pada Pendidikan Ibu, Pendidikan Ayah, dan Jenis Kelamin
Untuk ComboBox sganti properties name nya dengan
ComboBox1 ganti dengan CBOKelamin
ComboBox2 ganti dengan CBOPendidikanIbu
ComboBox3 ganti dengan CBOPendidikanAyah
ComboBox1 ganti dengan CBOKelamin
ComboBox2 ganti dengan CBOPendidikanIbu
ComboBox3 ganti dengan CBOPendidikanAyah
Textbox2 ganti dengan TXTNama
Textbox3 ganti dengan TXTTempatLahir
Textbox4 ganti dengan TXTTglLahir
Textbox5 ganti dengan TXTALamat
Textbox6 ganti dengan TXTNISN
Textbox7 ganti dengan TXTHP
Textbox8 ganti dengan TXTSKHUN
Textbox9 ganti dengan TXTIjasah
Textbox10 ganti dengan TXTNamaIbu
Textbox11 ganti dengan TXTThnLahirIbu
Textbox12 ganti dengan TXTPekIbu
Textbox13 ganti dengan TXTNamaAyah
Textbox14 ganti dengan TXTThnAyah
Textbox15 ganti dengan TXTPekAyah
Textbox16 ganti dengan TXTPengAyah
Textbox17 ganti dengan TXTAlamatOrtu
Gunakan CommandButton untuk Simpan, Cari Data Siswa, dan Close
Langkah selanjutnya, View Code dengan cara Klik Kanan pada Userform dan pilih view code.
dan akan muncul tampilan seperti ini:
Tuliskan script berikut untuk membuat initial pada UserForm
Private Sub UserForm_Initialize()
With CBOKelamin
.AddItem "Laki-Laki"
.AddItem "Perempuan"
End With
With CBOPendidikanIbu
.AddItem "Tidak Sekolah"
.AddItem "SD"
.AddItem "SMP"
.AddItem "SMA"
.AddItem "D1"
.AddItem "D2"
.AddItem "D3"
.AddItem "S1"
.AddItem "S2"
.AddItem "S3"
End With
With CBOPendidikanAyah
.AddItem "Tidak Sekolah"
.AddItem "SD"
.AddItem "SMP"
.AddItem "SMA"
.AddItem "D1"
.AddItem "D2"
.AddItem "D3"
.AddItem "S1"
.AddItem "S2"
.AddItem "S3"
End With
End Sub
With CBOKelamin
.AddItem "Laki-Laki"
.AddItem "Perempuan"
End With
With CBOPendidikanIbu
.AddItem "Tidak Sekolah"
.AddItem "SD"
.AddItem "SMP"
.AddItem "SMA"
.AddItem "D1"
.AddItem "D2"
.AddItem "D3"
.AddItem "S1"
.AddItem "S2"
.AddItem "S3"
End With
With CBOPendidikanAyah
.AddItem "Tidak Sekolah"
.AddItem "SD"
.AddItem "SMP"
.AddItem "SMA"
.AddItem "D1"
.AddItem "D2"
.AddItem "D3"
.AddItem "S1"
.AddItem "S2"
.AddItem "S3"
End With
End Sub
6. Ketikkan script VBA nya di bawah ini untuk script data siswa
Private Sub TBLSimpan_Click()
Dim iRow As Long
Dim Ws As Worksheet
Set Ws = Worksheets("databasesiswa")
'menemukan baris kosong pada database siswa
iRow = Ws.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row
'check untuk sebuah nis
If Trim(Me.TXTNis.Value) = "" Then
Me.TXTNis.SetFocus
MsgBox "Masukan NIS terlebih dahulu Kang.."
Exit Sub
End If
'copy data ke database siswa
Ws.Cells(iRow, 1).Value = Range("X1").Value
Ws.Cells(iRow, 2).Value = Me.TXTNis.Value
Ws.Cells(iRow, 3).Value = Me.TXTNama.Value
Ws.Cells(iRow, 4).Value = Me.TXTTempatLahir.Value
Ws.Cells(iRow, 5).Value = Me.TXTTglLahir.Value
Ws.Cells(iRow, 6).Value = Me.CBOKelamin.Value
Ws.Cells(iRow, 7).Value = Me.TXTAlamat.Value
Ws.Cells(iRow, 8).Value = Me.TXTNISN.Value
Ws.Cells(iRow, 9).Value = Me.TXTHP.Value
Ws.Cells(iRow, 10).Value = Me.TXTSKHUN.Value
Ws.Cells(iRow, 11).Value = Me.TXTIjasah.Value
Ws.Cells(iRow, 12).Value = Me.TXTNamaIbu.Value
Ws.Cells(iRow, 13).Value = Me.TXTThnLahirIbu.Value
Ws.Cells(iRow, 14).Value = Me.TXTPekIbu.Value
Ws.Cells(iRow, 15).Value = Me.CBOPendidikanIbu.Value
Ws.Cells(iRow, 16).Value = Me.TXTNamaAyah.Value
Ws.Cells(iRow, 17).Value = Me.TXTThnAyah.Value
Ws.Cells(iRow, 18).Value = Me.TXTPekAyah.Value
Ws.Cells(iRow, 19).Value = Me.CBOPendidikanAyah.Value
Ws.Cells(iRow, 20).Value = Me.TXTPengAyah.Value
Ws.Cells(iRow, 21).Value = Me.TXTAlamat.Value
'clear data siswa
Me.TXTNis.Value = ""
Me.TXTNama.Value = ""
Me.TXTTempatLahir.Value = ""
Me.TXTTglLahir.Value = ""
Me.CBOKelamin.Value = ""
Me.TXTAlamat.Value = ""
Me.TXTNISN.Value = ""
Me.TXTHP.Value = ""
Me.TXTSKHUN.Value = ""
Me.TXTIjasah.Value = ""
Me.TXTNamaIbu.Value = ""
Me.TXTThnLahirIbu.Value = ""
Me.TXTPekIbu.Value = ""
Me.CBOPendidikanIbu.Value = ""
Me.TXTNamaAyah.Value = ""
Me.TXTThnAyah.Value = ""
Me.TXTPekAyah.Value = ""
Me.CBOPendidikanAyah.Value = ""
Me.TXTPengAyah.Value = ""
Me.TXTAlamatOrtu.Value = ""
Me.TXTNis.SetFocus
'Simpan data
Application.ActiveWorkbook.Save
End Sub
Dim iRow As Long
Dim Ws As Worksheet
Set Ws = Worksheets("databasesiswa")
'menemukan baris kosong pada database siswa
iRow = Ws.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row
'check untuk sebuah nis
If Trim(Me.TXTNis.Value) = "" Then
Me.TXTNis.SetFocus
MsgBox "Masukan NIS terlebih dahulu Kang.."
Exit Sub
End If
'copy data ke database siswa
Ws.Cells(iRow, 1).Value = Range("X1").Value
Ws.Cells(iRow, 2).Value = Me.TXTNis.Value
Ws.Cells(iRow, 3).Value = Me.TXTNama.Value
Ws.Cells(iRow, 4).Value = Me.TXTTempatLahir.Value
Ws.Cells(iRow, 5).Value = Me.TXTTglLahir.Value
Ws.Cells(iRow, 6).Value = Me.CBOKelamin.Value
Ws.Cells(iRow, 7).Value = Me.TXTAlamat.Value
Ws.Cells(iRow, 8).Value = Me.TXTNISN.Value
Ws.Cells(iRow, 9).Value = Me.TXTHP.Value
Ws.Cells(iRow, 10).Value = Me.TXTSKHUN.Value
Ws.Cells(iRow, 11).Value = Me.TXTIjasah.Value
Ws.Cells(iRow, 12).Value = Me.TXTNamaIbu.Value
Ws.Cells(iRow, 13).Value = Me.TXTThnLahirIbu.Value
Ws.Cells(iRow, 14).Value = Me.TXTPekIbu.Value
Ws.Cells(iRow, 15).Value = Me.CBOPendidikanIbu.Value
Ws.Cells(iRow, 16).Value = Me.TXTNamaAyah.Value
Ws.Cells(iRow, 17).Value = Me.TXTThnAyah.Value
Ws.Cells(iRow, 18).Value = Me.TXTPekAyah.Value
Ws.Cells(iRow, 19).Value = Me.CBOPendidikanAyah.Value
Ws.Cells(iRow, 20).Value = Me.TXTPengAyah.Value
Ws.Cells(iRow, 21).Value = Me.TXTAlamat.Value
'clear data siswa
Me.TXTNis.Value = ""
Me.TXTNama.Value = ""
Me.TXTTempatLahir.Value = ""
Me.TXTTglLahir.Value = ""
Me.CBOKelamin.Value = ""
Me.TXTAlamat.Value = ""
Me.TXTNISN.Value = ""
Me.TXTHP.Value = ""
Me.TXTSKHUN.Value = ""
Me.TXTIjasah.Value = ""
Me.TXTNamaIbu.Value = ""
Me.TXTThnLahirIbu.Value = ""
Me.TXTPekIbu.Value = ""
Me.CBOPendidikanIbu.Value = ""
Me.TXTNamaAyah.Value = ""
Me.TXTThnAyah.Value = ""
Me.TXTPekAyah.Value = ""
Me.CBOPendidikanAyah.Value = ""
Me.TXTPengAyah.Value = ""
Me.TXTAlamatOrtu.Value = ""
Me.TXTNis.SetFocus
'Simpan data
Application.ActiveWorkbook.Save
End Sub
7. Membuat perintah CLOSE silakan ketikkan script nya dibawah ini dengan cara double klik tombol CLOSE
Private Sub CMDClose_Click()
Unload Me
End Sub
Unload Me
End Sub
8. Membuat script agar apabila data yang dimasukan pada textbox bukan berupa data angka maka secara otomatis akan menampilkan pesan error
Private Sub HanyaAngka()
If TypeName(Me.ActiveControl) = "TextBox" Then
With Me.ActiveControl
If Not IsNumeric(.Value) And .Value <> vbNullString Then
MsgBox "Maaf, Masukan data angka saja"
.Value = vbNullString
End If
End With
End If
End Sub
If TypeName(Me.ActiveControl) = "TextBox" Then
With Me.ActiveControl
If Not IsNumeric(.Value) And .Value <> vbNullString Then
MsgBox "Maaf, Masukan data angka saja"
.Value = vbNullString
End If
End With
End If
End Sub
9. Untuk membuat perubahan warna ketika textbox dipilih
Private Sub TXTNISN_Change()
HanyaAngka
End Sub
Private Sub TXTHP_Change()
HanyaAngka
End Sub
Private Sub txtnis_Enter()
TXTNis.BackColor = &H80000005
End Sub
Private Sub txtnis_Exit(ByVal Cancel As MSForms.ReturnBoolean)
HanyaAngka
TXTNis.BackColor = &HE0E0E0
End Sub
Private Sub txtnama_enter()
TXTNama.BackColor = &H80000005
End Sub
Private Sub txtnama_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTNama.BackColor = &HE0E0E0
End Sub
Private Sub txttempatlahir_enter()
TXTTempatLahir.BackColor = &H80000005
End Sub
Private Sub txttempatlahir_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTTempatLahir.BackColor = &HE0E0E0
End Sub
Private Sub txttgllahir_enter()
TXTTglLahir.BackColor = &H80000005
End Sub
Private Sub txttgllahir_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTTglLahir.BackColor = &HE0E0E0
End Sub
Private Sub txtalamat_Enter()
TXTAlamat.BackColor = &H80000005
End Sub
Private Sub txtalamat_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTAlamat.BackColor = &HE0E0E0
End Sub
Private Sub cbokelamin_Enter()
CBOKelamin.BackColor = &H80000005
End Sub
Private Sub cbokelamin_Exit(ByVal Cancel As MSForms.ReturnBoolean)
CBOKelamin.BackColor = &HE0E0E0
End Sub
Private Sub txtnisn_Enter()
TXTNISN.BackColor = &H80000005
End Sub
Private Sub txtnisn_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTNISN.BackColor = &HE0E0E0
End Sub
Private Sub txthp_Enter()
TXTHP.BackColor = &H80000005
End Sub
Private Sub txthp_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTHP.BackColor = &HE0E0E0
End Sub
Private Sub txtskhun_Enter()
TXTSKHUN.BackColor = &H80000005
End Sub
Private Sub txtskhun_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTSKHUN.BackColor = &HE0E0E0
End Sub
Private Sub txtijasah_Enter()
TXTIjasah.BackColor = &H80000005
End Sub
Private Sub txtijasah_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTIjasah.BackColor = &HE0E0E0
End Sub
Private Sub txtnamaibu_Enter()
TXTNamaIbu.BackColor = &H80000005
End Sub
Private Sub txtnamaibu_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTNamaIbu.BackColor = &HE0E0E0
End Sub
Private Sub txtthnlahiribu_Enter()
TXTThnLahirIbu.BackColor = &H80000005
End Sub
Private Sub txtthnlahiribu_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTThnLahirIbu.BackColor = &HE0E0E0
End Sub
Private Sub txtpekibu_Enter()
TXTPekIbu.BackColor = &H80000005
End Sub
Private Sub txtpekibu_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTPekIbu.BackColor = &HE0E0E0
End Sub
Private Sub cbopendidikanibu_Enter()
CBOPendidikanIbu.BackColor = &H80000005
End Sub
Private Sub cbopendidikanibu_Exit(ByVal Cancel As MSForms.ReturnBoolean)
CBOPendidikanIbu.BackColor = &HE0E0E0
End Sub
Private Sub txtnamaayah_Enter()
TXTNamaAyah.BackColor = &H80000005
End Sub
Private Sub txtnamaayah_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTNamaAyah.BackColor = &HE0E0E0
End Sub
Private Sub txtthnayah_Enter()
TXTThnAyah.BackColor = &H80000005
End Sub
Private Sub txtthnayah_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTThnAyah.BackColor = &HE0E0E0
End Sub
Private Sub txtpekayah_Enter()
TXTPekAyah.BackColor = &H80000005
End Sub
Private Sub txtpekayah_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTPekAyah.BackColor = &HE0E0E0
End Sub
Private Sub cbopendidikanayah_Enter()
CBOPendidikanAyah.BackColor = &H80000005
End Sub
Private Sub cbopendidikanayah_Exit(ByVal Cancel As MSForms.ReturnBoolean)
CBOPendidikanAyah.BackColor = &HE0E0E0
End Sub
Private Sub txtpengayah_Enter()
TXTPengAyah.BackColor = &H80000005
End Sub
Private Sub txtpengayah_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTPengAyah.BackColor = &HE0E0E0
End Sub
Private Sub txtalamatortu_Enter()
TXTAlamatOrtu.BackColor = &H80000005
End Sub
Private Sub txtalamatortu_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTAlamatOrtu.BackColor = &HE0E0E0
End Sub
HanyaAngka
End Sub
Private Sub TXTHP_Change()
HanyaAngka
End Sub
Private Sub txtnis_Enter()
TXTNis.BackColor = &H80000005
End Sub
Private Sub txtnis_Exit(ByVal Cancel As MSForms.ReturnBoolean)
HanyaAngka
TXTNis.BackColor = &HE0E0E0
End Sub
Private Sub txtnama_enter()
TXTNama.BackColor = &H80000005
End Sub
Private Sub txtnama_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTNama.BackColor = &HE0E0E0
End Sub
Private Sub txttempatlahir_enter()
TXTTempatLahir.BackColor = &H80000005
End Sub
Private Sub txttempatlahir_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTTempatLahir.BackColor = &HE0E0E0
End Sub
Private Sub txttgllahir_enter()
TXTTglLahir.BackColor = &H80000005
End Sub
Private Sub txttgllahir_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTTglLahir.BackColor = &HE0E0E0
End Sub
Private Sub txtalamat_Enter()
TXTAlamat.BackColor = &H80000005
End Sub
Private Sub txtalamat_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTAlamat.BackColor = &HE0E0E0
End Sub
Private Sub cbokelamin_Enter()
CBOKelamin.BackColor = &H80000005
End Sub
Private Sub cbokelamin_Exit(ByVal Cancel As MSForms.ReturnBoolean)
CBOKelamin.BackColor = &HE0E0E0
End Sub
Private Sub txtnisn_Enter()
TXTNISN.BackColor = &H80000005
End Sub
Private Sub txtnisn_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTNISN.BackColor = &HE0E0E0
End Sub
Private Sub txthp_Enter()
TXTHP.BackColor = &H80000005
End Sub
Private Sub txthp_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTHP.BackColor = &HE0E0E0
End Sub
Private Sub txtskhun_Enter()
TXTSKHUN.BackColor = &H80000005
End Sub
Private Sub txtskhun_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTSKHUN.BackColor = &HE0E0E0
End Sub
Private Sub txtijasah_Enter()
TXTIjasah.BackColor = &H80000005
End Sub
Private Sub txtijasah_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTIjasah.BackColor = &HE0E0E0
End Sub
Private Sub txtnamaibu_Enter()
TXTNamaIbu.BackColor = &H80000005
End Sub
Private Sub txtnamaibu_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTNamaIbu.BackColor = &HE0E0E0
End Sub
Private Sub txtthnlahiribu_Enter()
TXTThnLahirIbu.BackColor = &H80000005
End Sub
Private Sub txtthnlahiribu_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTThnLahirIbu.BackColor = &HE0E0E0
End Sub
Private Sub txtpekibu_Enter()
TXTPekIbu.BackColor = &H80000005
End Sub
Private Sub txtpekibu_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTPekIbu.BackColor = &HE0E0E0
End Sub
Private Sub cbopendidikanibu_Enter()
CBOPendidikanIbu.BackColor = &H80000005
End Sub
Private Sub cbopendidikanibu_Exit(ByVal Cancel As MSForms.ReturnBoolean)
CBOPendidikanIbu.BackColor = &HE0E0E0
End Sub
Private Sub txtnamaayah_Enter()
TXTNamaAyah.BackColor = &H80000005
End Sub
Private Sub txtnamaayah_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTNamaAyah.BackColor = &HE0E0E0
End Sub
Private Sub txtthnayah_Enter()
TXTThnAyah.BackColor = &H80000005
End Sub
Private Sub txtthnayah_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTThnAyah.BackColor = &HE0E0E0
End Sub
Private Sub txtpekayah_Enter()
TXTPekAyah.BackColor = &H80000005
End Sub
Private Sub txtpekayah_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTPekAyah.BackColor = &HE0E0E0
End Sub
Private Sub cbopendidikanayah_Enter()
CBOPendidikanAyah.BackColor = &H80000005
End Sub
Private Sub cbopendidikanayah_Exit(ByVal Cancel As MSForms.ReturnBoolean)
CBOPendidikanAyah.BackColor = &HE0E0E0
End Sub
Private Sub txtpengayah_Enter()
TXTPengAyah.BackColor = &H80000005
End Sub
Private Sub txtpengayah_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTPengAyah.BackColor = &HE0E0E0
End Sub
Private Sub txtalamatortu_Enter()
TXTAlamatOrtu.BackColor = &H80000005
End Sub
Private Sub txtalamatortu_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TXTAlamatOrtu.BackColor = &HE0E0E0
End Sub
Tahap terakhir, rename Sheet1 dengan 'DataBaseSiswa' untuk menyimpan database. Maka, ketika database disimpan, akan tersimpan pada sheet tsb.
Untuk menjalankan userformnya pilih menu Run lalu Runsub/Userform
0 komentar:
Posting Komentar