Anda di halaman 1dari 4

Kumpulan Kode VBA Excel Dasar

VBA merupakan Visual Basic of Application dimana VBA ini sangat membantu sekali dalam
mengolah data Excel. Jika Anda mahir Excel maka akan sangat hebat apabila dikolaborasikan
dengan kode-kode VBA. Jika Anda baru belajar tentang kode-kode VBA berikut contoh dasar
kode VBA excel bagi pemula.

Selecting

'==Kode VBA Memilih 2 baris ke bawah dan 2 kolom ke kanan:


ActiveCell.Offset(3, 2).Select

'==Kode VBA Pilih dari sel aktif ke baris terakhir dari daftar:
Range(Selection, Selection.End(xlDown)).Select
'==Kode VBA Pilih dari sel saat ini untuk kolom terakhir dari daftar:
Range(Selection, Selection.End(xlToRight)).Select
'==Kode VBA Pilih sel terakhir dari worksheet:
Selection.SpecialCells(xlLastCell).Select

Pasting

'==Kode VBA Nilai rumus Paste, bukan rumus:


Range("A3").Copy
Range("D26").PasteSpecial Paste:=xlValues
'==Kode VBA Paste ke dalam sel dan memindahkan isi aslinya ke sel berikutnya:
Selection.Insert Shift:=xlToRight

Columns and Rows


'==Kode VBA Menyembuntikan Kolom:
Selection.EntireColumn.Hidden = True
'==Kode VBA Menyisipkan Kolom:
Columns("N:N").Insert
'==Kode VBA Menghapus Kolom:
Columns("B:E").EntireColumn.Delete
'==Kode VBA Menyisipkan baris baru di cell saat ini:
Selection.EntireRow.Insert
'==Kode VBA menghapus baris baru di cell saat ini:
Selection.EntireRow.Delete
'==Kode VBA Mengatur lebar kolom:
Selection.EntireColumn.ColumnWidth = 10
'==Kode VBA Mengatur tinggi baris
Selection.RowHeight = 26.25
'==Kode VBA Mengatur tinggi baris dengan ukuran isi:
Selection.Rows.AutoFit

Cell Formatting

'==Kode VBA Wrap Teks


Selection.WrapText = False
'==Kode VBA Mengahpus warna
Selection.Interior.ColorIndex = xlNone
'==Kode VBA mengatur ukuran huruf
Selection.Font.Size = 8
'==Kode VBA mengatur format tanggal dan waktu
Selection.NumberFormat = "mm-dd-yyyy hh:mm AM/PM"
'==Kode VBA mengatur angka desimal/pecahan
Selection.NumberFormat = "#,##0"
'==Kode VBA perataan tengah
Selection.HorizontalAlignment = xlLeft
'==Kode VBA rata bawah
Selection.VerticalAlignment = xlBottom
'==Indented text:
Selection.IndentLevel = 3
'==Kode VBA Hapus isi tapi tidak format:
Selection.ClearContents
'==Kode VBA Hapus isi dan format:
Selection.Clear
Display

'==Kode VBA Menyembunyikan aktivitas sementara makro berjalan:


Application.ScreenUpdating = False
'==Kode VBA Matikan lansiran otomatis:
Application.DisplayAlerts = False
'==Kode VBA untuk Freeze panes:
ActiveWindow.FreezePanes = True
'==Kode VBA Tampilkan berjalan makro berapa lama:
Dim strTime1 as String, strTime2 as String
strTime1 = Format(Now(), "mm-dd-yyyy hh:MM:ss")
[put other macro code here]
strTime2 = Format(Now(), "mm-dd-yyyy hh:MM:ss")
MsgBox "Elapsed Time = " & DateDiff("n", strTime1, strTime2)

Range Names

'==Kode VBA menamai range


ActiveWorkbook.Names.Add Name:="Groups", RefersTo:=Selection
'==Kode VBA menuju ke alamat range
Range("Groups").Select
'==kode VBA menghapus nama range workbook aktif
Dim n as Object
For Each n In ActiveWorkbook.Names
n.Delete
Next

Path/File Name

'==Kode VBA menyisimpan nama lokasi file


ActiveCell.Value = ActiveWorkbook.FullName
'==kode VBA untuk Insert path/file kedalam footer

ActiveSheet.PageSetup.CenterFooter = ActiveWorkbook.FullName

Pivot Tables

'===Kode VBA Menghapus item yang tidak terpakai di tabel pivot ketika data telah berubah:
Dim pt As PivotTable, ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
For Each pt In ws.PivotTables
pt.PivotCache.MissingItemsLimit = xlMissingItemsNone
Next pt
Next ws

Worksheets

'==Kode VBA Menambahkan tanggal untuk judul setiap worksheet:


Dim sht As Worksheet
For Each sht In ActiveWorkbook.Worksheets
sht.Select
Range("A1").Value = Range("A1").Value & " through " & strDate
Next sht

Semoga bermanfaat. dan untuk melihat contoh penggunaan kode VBA lebih lengkap silakan
lihat contoh Pembuatan Input Data dengan VBA

Anda mungkin juga menyukai