Menu

Rabu, 08 Oktober 2014

MEMBUAT ENTRI CARI SIMPAN EDIT HAPUS DENGAN MACRO EXCEL

Private Sub cmdCari_Click()
Dim KodeSiswa
Dim CellTujuan As Range

KodeSiswa = txtKodeSiswa.Text
Set CellTujuan = Range("B:B").Find(What:=KodeSiswa)

If Not CellTujuan Is Nothing Then
txtNamaSiswa.Text = Cells(CellTujuan.Row, 3)
cmbProgramStudi.Text = Cells(CellTujuan.Row, 4)
If Cells(CellTujuan.Row, 5) = "Laki-laki" Then
optLakiLaki.Value = True
ElseIf Cells(CellTujuan.Row, 5) = "Perempuan" Then optPerempuan.Value = True
End If
txtTempatLahir.Text = Cells(CellTujuan.Row, 6)
Else
MsgBox "Tidak Ada Hasil !"
End If
End Sub

Private Sub cmdTambah_Click() 
    Dim baris As Integer
                baris = WorksheetFunction.CountA(Range("B:B"))

baris = baris + 1

               Cells(baris, 2) = txtKodeSiswa 
       Cells(baris, 3) = txtNamaSiswa 
       Cells(baris, 4) = cmbProgramStudi
       If optLakiLaki = True Then 
       Cells(baris, 5) = "Laki-laki" 
       ElseIf optPerempuan = True Then
Cells(baris, 5) = "Perempuan" End If
Cells(baris, 6) = txtTempatLahir

Cells(baris, 7) = txtTanggalLahir
  End Sub

Private Sub cmdUbah_Click() Dim KodeSiswa
Dim CellTujuan As Range

                KodeSiswa = txtKodeSiswa.Text

Set CellTujuan = Range("B:B").Find(What:=KodeSiswa)

  
Cells(CellTujuan.Row, 2) = txtKodeSiswa Cells(CellTujuan.Row, 3) = txtNamaSiswa Cells(CellTujuan.Row, 4) = cmbProgramStudi If optLakiLaki = True Then Cells(CellTujuan.Row, 5) = "Laki-laki" ElseIf optPerempuan = True Then Cells(CellTujuan.Row, 5) = "Perempuan"
End If
Cells(CellTujuan.Row, 6) = txtTempatLahir Cells(CellTujuan.Row, 7) =txtTanggalLahir End Sub

Private Sub cmdHapus_Click() Dim KodeSiswa
Dim CellTujuan As Range

KodeSiswa = txtKodeSiswa.Text
Set CellTujuan =Range("B:B").Find(What:=KodeSiswa) Rows(CellTujuan.Row).Delete Shift:=xlUp
End Sub



RUMUS MENCARI BARIS ATAU KOLOM YANG KOSONG DENGAN MACRO EXCEL

Private Sub BarisKosong_satu()
Dim BarisKosong
BarisKosong = Cells(1, 1).End(xlDown).Offset(1, 0).Row
MsgBox "cara satu :" & BarisKosong
End Sub

Private Sub BarisKosong_dua()
Dim BarisKosong
BarisKosong = Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
MsgBox "cara dua :" & BarisKosong
End Sub

Private Sub BarisKosong_tiga()
Dim BarisKosong
Dim i
i = 1
Do While Cells(i, 1) <> ""
i = i + 1
Loop
BarisKosong = i
MsgBox "cara tiga :" & BarisKosong
End Sub


Private Sub BarisKosong_empat()
Dim BarisKosong
Dim i
i = 1
Do While Not IsEmpty(Cells(i, 1))
i = i + 1
Loop
BarisKosong = i
MsgBox "cara empat :" & BarisKosong
End Sub

Private Sub BarisKosong_lima() 'prosesnya lama
Dim BarisKosong
Dim i
i = Rows.Count
Do While IsEmpty(Cells(i, 1))
i = i - 1
Loop
BarisKosong = i + 1
MsgBox "cara lima :" & BarisKosong
End Sub

Private Sub BarisKosong_enam()
Dim BarisKosong
Dim i
i = 1
Do While Not IsEmpty(Cells(i, 1))
BarisKosong = Cells(i, 1).Offset(1, 0).Row
i = i + 1
Loop
'BarisKosong = i + 1
MsgBox "cara enam :" & BarisKosong
End Sub

Private Sub BarisKosong_tujuh() 'prosesnya lama
Dim BarisKosong
Dim i
i = Rows.Count
Do While IsEmpty(Cells(i, 1))
BarisKosong = Cells(i, 1).Offset(-1, 0).Row
i = i - 1
Loop
BarisKosong = BarisKosong + 1
MsgBox "cara tujuh :" & BarisKosong
End Sub

'============================
Private Sub kolomKosong_satu()
Dim kolomKosong
kolomKosong = Cells(1, 1).End(xlToRight).Offset(0, 1).Column
MsgBox "cara satu :" & kolomKosong
End Sub

Private Sub kolomKosong_dua()
Dim kolomKosong
kolomKosong = Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1).Column
MsgBox "cara dua :" & kolomKosong
End Sub

'silakan berkreasi sendiri semoga sukses

'secara manual cari baris kosong adalah sebagai berikut :
'1. pilih cels A1
'2. tekan enter atau panah bawah terus sampai ketemu baris yang kosong
'kalau dibahasakan dengan VBA
'Cells(1,1) = meletakkan poniter ke cell A1
'.End = sampai mentok/akhir
'(xlDown)=panah atas
'offset = geser
'(1,0)=argument gesernya =1 baris ke bawah, 0 kolom ke kanan artinya tetap dikolom A
' .Row = mengambil nilai baris

'baris kosong = Cells(1, 1).End(xlDown).Offset(1, 0).Row
'artinya seolah kita pilih cell A1 kemudian tekan panah ke bawah sampai baris terakhir
'yang ada isinya, kemudian turun satu baris lagi,
'kemudian nilai barisnya disimpan dalam variabel yang bernama "BarisKosong"
' maka inilah baris kosong pertama

'Lain lagi jika 'baris kosong = Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
'ini artinya seolah kita pilih cells kolom A paling bawah terus tekan panah atas
'sampai mentok ketemu cell yang tidak kosong kemudian geser ke bawah 1 baris,
'kemudian nilai barisnya disimpan dalam variabel yang bernama "BarisKosong"

'rows.count = menunjukkan bnyak baris dalam worksheet, nilainya tergantung versi excel/ms office nya
'kalau versi 2003=
'kalau versi 2007 = 1048576
'dll

'columns.Count =menunjukkan jumlah kolom dalam worksheet, nilai juga tergantu versinya
'kalau versi 2007 = 16384
'
'dengan cara yang sama di atas kode berikut ini juga dipahami
'cuma berbeda arahnya
'kolomKosong = Cells(1, 1).End(xlToRight).Offset(0, 1).Column
'kolomKosong = Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1).Column

' xlUp = panah ke atas
' xlDown = panah ke bawah
' xlToRight = panah ke kanan
' xlToLeft = panah ke kiri
'
' Do while
'......
'......
' Loop

' adalah perintah untuk iterasi/perulangan
' artinya kode-kode di antara Do While ... Loop akan terus dijalankan jika syarat kondisinya terpenuhi
' syarat kondisi diletakkan setelah kata "while" tersebut
' Do While cells(i,1)<>"" artinya kerjakan selama cells(baris i kolom 1) tidak kosong
' sama maksudnya dengan perintah Do While Not IsEmpty