Anda di halaman 1dari 6

26/12/2018 Kumpulan Kode VBA Excel Dasar Yang Wajib Dipelajari | 300 Rumus Excel

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:

'==Kode VBA Pilih dari sel aktif ke baris terakhir


ActiveCell.Offset(3, 2).Select dari daftar:

Range(Selection, Selection.End(xlDown)).Select

'==Kode VBA Pilih dari sel saat ini untuk kolom terakhir dari daftar:
'==Kode VBA Pilih sel terakhir dari worksheet:

Range(Selection, Selection.End(xlToRight)).Select
Selection.SpecialCells(xlLastCell).Select

Pasting
'==Kode VBA Nilai rumus Paste, bukan rumus:
'==Kode VBA Paste ke dalam sel dan
memindahkan isi aslinya ke sel berikutnya:
Range("A3").Copy

Range("D26").PasteSpecial Paste:=xlValues
Selection.Insert Shift:=xlToRight

Columns and Rows


'==Kode VBA Menyembuntikan Kolom:
'==Kode VBA Menyisipkan Kolom:

Selection.EntireColumn.Hidden = True
Columns("N:N").Insert

'==Kode VBA Menghapus Kolom:


'==Kode VBA Menyisipkan baris baru di cell saat
ini:
Columns("B:E").EntireColumn.Delete

Selection.EntireRow.Insert

'==Kode VBA menghapus baris baru di cell saat ini:


'==Kode VBA Mengatur lebar kolom:
http://www.excel-id.com/2015/09/kumpulan-kode-vba-excel-dasar-yang.html 1/6
26/12/2018 Kumpulan Kode VBA Excel Dasar Yang Wajib Dipelajari | 300 Rumus Excel

Selection.EntireRow.Delete Selection.EntireColumn.ColumnWidth = 10

'==Kode VBA Mengatur tinggi baris


'==Kode VBA Mengatur tinggi baris dengan
ukuran isi:
Selection.RowHeight = 26.25

Selection.Rows.AutoFit

Cell Formatting
'==Kode VBA Wrap Teks
'==Kode VBA Mengahpus warna

Selection.WrapText = False
Selection.Interior.ColorIndex = xlNone

'==Kode VBA mengatur ukuran huruf


'==Kode VBA mengatur format tanggal dan
waktu
Selection.Font.Size = 8

Selection.NumberFormat = "mm-dd-yyyy hh:mm

AM/PM"

'==Kode VBA mengatur angka desimal/pecahan


'==Kode VBA perataan tengah

Selection.NumberFormat = "#,##0"
Selection.HorizontalAlignment = xlLeft

'==Kode VBA rata bawah


'==Indented text:

Selection.VerticalAlignment = xlBottom
Selection.IndentLevel = 3

'==Kode VBA Hapus isi tapi tidak format:


'==Kode VBA Hapus isi dan format:

Selection.ClearContents
Selection.Clear

Display
http://www.excel-id.com/2015/09/kumpulan-kode-vba-excel-dasar-yang.html 2/6
26/12/2018 Kumpulan Kode VBA Excel Dasar Yang Wajib Dipelajari | 300 Rumus Excel

'==Kode VBA Menyembunyikan aktivitas sementara makro berjalan:


'==Kode VBA Matikan lansiran otomatis:

Application.ScreenUpdating = False
Application.DisplayAlerts = False

'==Kode VBA untuk Freeze panes:


'==Kode VBA Tampilkan berjalan makro berapa
lama:
ActiveWindow.FreezePanes = True

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
'==Kode VBA menuju ke alamat range

ActiveWorkbook.Names.Add Name:="Groups",
Range("Groups").Select
RefersTo:=Selection

'==kode VBA menghapus nama range workbook


aktif

Dim n as Object
Path/File Name
For Each n In ActiveWorkbook.Names
'==Kode VBA menyisimpan nama lokasi file
n.Delete
Next ActiveCell.Value = ActiveWorkbook.FullName

'==kode VBA untuk Insert path/file kedalam footer

ActiveSheet.PageSetup.CenterFooter =
Pivot Tables
ActiveWorkbook.FullName
'===Kode VBA Menghapus item yang tidak
terpakai di tabel pivot ketika data telah berubah:

http://www.excel-id.com/2015/09/kumpulan-kode-vba-excel-dasar-yang.html 3/6
26/12/2018 Kumpulan Kode VBA Excel Dasar Yang Wajib Dipelajari | 300 Rumus Excel

Worksheets
Dim pt As PivotTable, ws As Worksheet '==Kode VBA Menambahkan tanggal untuk judul
setiap worksheet:
For Each ws In ActiveWorkbook.Worksheets

For Each pt In ws.PivotTables


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

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

UPDATE : Koleksi Kode VBA untuk Belajar Macro Dasar


12/12/2015

Coding untuk Copy Paste Range

Kode Menampilkan Posisi Cell yang Aktif


Sub CopyRange( )
Range (“A1:B10”).Copy
Sub AlamatCell( )
Destination:=Range(“D1:E10”)
baris = ActiveCell.Row
End Sub
kolom = ActiveCell.Column
Msgbox baris & “,” & kolom
End Sub

Kode VBA untuk format tebal huruf dan pewarnaan

Coding VBA merubah Properties Listbox


Sub MerubahFont( ) (Jumlah kolom, Rowsource, Jumlah kolom
Selection.Font.Bold = True ' untuk menebalkan huruf dan ukuran kolom.
Selection.Font.ColorIndex = 3 ' untuk mewarna
merah Private Sub UserForm_Initialize()
End Sub With UserForm1.ListBox1
.RowSource = "ListData"
.ColumnCount = 5
.ColumnWidths = "37,95,60,60,50"

http://www.excel-id.com/2015/09/kumpulan-kode-vba-excel-dasar-yang.html 4/6
26/12/2018 Kumpulan Kode VBA Excel Dasar Yang Wajib Dipelajari | 300 Rumus Excel

End With

End Sub

Coding Menuju Sheet tertentu

atau bisa juga gunakan


Sheets(1).Select

Sheets1.Select

Coding VBA menyembunyikan Sheet1


Contoh Lain : Sheets("Sheet1").Select

Sheet1.Visible = xlSheetVeryHidden

Coding VBA menampilkan Input Box

Contoh Coding Menyisipkan atau


InputBox(“Silakan Masukan Nama Anda”) menambah baris diatas range tertentu

Range(“A1”).Select ' Pilih range

Selection.EntireRow.Insert ' sisipkan baris diatas


range A1

Coding VBA menyimpan nama file

Contoh Coding Format Tanggal pada


ActiveWorkbook.SaveAs
Textbox
Filename:=”D:\LatihanVBA.xls”

Private Sub TextBox1_Change()


TextBox1.Value = Format(TextBox1.Value,
"dd/mm/yyyy")

End Sub

http://www.excel-id.com/2015/09/kumpulan-kode-vba-excel-dasar-yang.html 5/6
26/12/2018 Kumpulan Kode VBA Excel Dasar Yang Wajib Dipelajari | 300 Rumus Excel

Coding Copy Listbox1 ke Listbox 2

Coding VBA menampilkan Listbox ke


Dim i As Long Textbox
Dim j As Long Contohnya kita mempunya data pada listbox
dengan 5 kolom dan masing-masing data akan
ditampilkan pada textbox maka kita harus
ListBox2.ColumnCount = ListBox1.ColumnCount mempunya textbox sebanyak 5 kolom juga dan
For i = 0 To ListBox1.ListCount - 1
berikut coding VBA nya

If ListBox1.Selected(i) Then
Private Sub ListBox1_AfterUpdate()
With ListBox2
Me.TextBox1.Value = Me.ListBox1.Column(0)
.AddItem
Me.TextBox2.Value = Me.ListBox1.Column(1)
For j = 0 To .ColumnCount - 1
Me.TextBox3.Value = Me.ListBox1.Column(2)
.List(.ListCount - 1, j) = ListBox1.List(i,
Me.TextBox4.Value = Me.ListBox1.Column(3)
j)
Me.TextBox5.Value = Me.ListBox1.Column(4)
Next j
End Sub
End With
End If
Next i Oke, segitu ajah dulu koleksi coding VBA
lengkap dan akan terus dilengkapi contoh-contoh
coding dasar VBA. Tunggu ajah ya update nya..

http://www.excel-id.com/2015/09/kumpulan-kode-vba-excel-dasar-yang.html 6/6

Anda mungkin juga menyukai