Dasar Vba
Dasar Vba
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
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