Anda di halaman 1dari 5

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
UPDATE : Koleksi Kode VBA untuk Belajar Macro Dasar
12/12/2015

Coding untuk Copy Paste Range


Sub CopyRange( )
Range (“A1:B10”).Copy Destination:=Range(“D1:E10”)
End Sub

Kode Menampilkan Posisi Cell yang Aktif


Sub AlamatCell( )
baris = ActiveCell.Row
kolom = ActiveCell.Column
Msgbox baris & “,” & kolom
End Sub

Kode VBA untuk format tebal huruf dan pewarnaan


Sub MerubahFont( )
Selection.Font.Bold = True ‘ untuk menebalkan huruf
Selection.Font.ColorIndex = 3 ‘ untuk mewarna merah
End Sub

Coding VBA merubah Properties Listbox (Jumlah kolom,


Rowsource, Jumlah kolom dan ukuran kolom.
Private Sub UserForm_Initialize()
With UserForm1.ListBox1
.RowSource = “ListData”
.ColumnCount = 5
.ColumnWidths = “37,95,60,60,50”
End With
End Sub

Coding Menuju Sheet tertentu


Sheets(1).Select
atau bisa juga gunakan
Sheets1.Select
 
Contoh Lain : Sheets(“Sheet1”).Select

Coding VBA menyembunyikan Sheet1


Sheet1.Visible = xlSheetVeryHidden

Coding VBA menampilkan Input Box


InputBox(“Silakan Masukan Nama Anda”)

Contoh Coding Menyisipkan atau menambah baris diatas range


tertentu
Range(“A1”).Select ‘ Pilih range
Selection.EntireRow.Insert ‘ sisipkan baris diatas range A1

Coding VBA menyimpan nama file


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

Contoh Coding Format Tanggal pada Textbox


Private Sub TextBox1_Change()
TextBox1.Value = Format(TextBox1.Value, “dd/mm/yyyy”)
End Sub

Coding Copy Listbox1 ke Listbox 2


Dim i As Long
Dim j As Long
ListBox2.ColumnCount = ListBox1.ColumnCount
For i = 0 To ListBox1.ListCount – 1
If ListBox1.Selected(i) Then
With ListBox2
.AddItem
For j = 0 To .ColumnCount – 1
.List(.ListCount – 1, j) = ListBox1.List(i, j)
Next j
End With
End If
Next i

Coding VBA menampilkan Listbox ke Textbox


Contohnya kita mempunya data pada listbox dengan 5 kolom dan masing-masing data akan ditampilkan
pada textbox maka kita harus mempunya textbox sebanyak 5 kolom juga dan berikut coding VBA nya
Private Sub ListBox1_AfterUpdate()
Me.TextBox1.Value = Me.ListBox1.Column(0)
Me.TextBox2.Value = Me.ListBox1.Column(1)
Me.TextBox3.Value = Me.ListBox1.Column(2)
Me.TextBox4.Value = Me.ListBox1.Column(3)
Me.TextBox5.Value = Me.ListBox1.Column(4)
End Sub

Anda mungkin juga menyukai