Object, Properties, Methods, dan Events
Object
VBA merupakan pemrograman berbasis Object (OOP), artinya semua adalah
Object, kita adalah Object, Alam semesta adalah Obyek, Excel, workbook,
worksheet, range, cell adalah object
Contoh : Obyek hirarki dari font object
- Disini terlihat bahwa excel/application obyek memiliki sub objek workbook
- Workbook memiliki member a.l: worksheet
- Worksheet memiliki sub object range
- Range memiliki object font, dst..dst….
Collection
Collection artinya sekumpulan obyek yang sama, misalnya :
- workbooks artinya sekumpulan workbook (misalnya kita membuka lebih dari 1 workbook dalam suatu waktu)
- Worksheets artinya sekumpulan worksheet dalam 1 workbook (jika dalam workbook kita memiliki lebih dari satu sheet)
- Cells sekumpulan cell
Untuk merujuk ke suatu obyek diantara collection/ kumpulan kita gunakan nama atau indexnya sebagai petunjuk, misalnya :
- workbooks(“myfile.xlsx”)
- Workbooks(1)
- Worksheets(5) artinya worksheet ke 5 dalam 1 workbook
Merujuk ke Object
- Kita bisa merujuk ke suatu object dengan cara bertingkat, misalnya
- Workbooks(“book1.xlsx”).Worksheets(“sheet1”).range(“A1”)
- Jika obyek sebelumnya tidak ditulis maka Excel mengasumsikan bahwa
obyek yang aktif adalah yang dimaksud, jadi bila workbook yang aktif
adalah “book1.xlsx” serta sheet yang aktif adalah “sheet1” maka kita
cukup menulis range(“A1”)
- Workbooks artinya sekumpulan (Collection) workbook yang terbuka pada
saat ini, kalo worksheets juga berarti collection dari worksheet (tanpa
“s”/jamak). Untuk merujuk ke worbook tertentu maka bisa dipakai nama
dari workbook (book1.xlsx) atau index (1,2,3,….)
Properties
Properties merupakan karakteristik dari suatu object. Misalnya
kalau Object itu kita, maka kita punya umur, tinggi, berat, warna kulit,
dll. Begitu juga misalnya Object workbook memiliki nama, path, saved,
dll
Di VBA kita menggunakan properties untuk mengambil nilainya ataupun mengubah nilainya, misalnya :
- Mengetahui apakah suatu file/workbook sudah disave atau belum (saved)
- Mengisi suatu cell dengan nilai tertentu (value property)
- mengubah font dari suatu cells (font property)
- dll
Untuk mengubah property kita bisa gunakan code ataupun visual
(properties windows) , misalnya mengubah nama worksheet
“mysheet” menjadi “your sheet” kita bisa gunakan code
worksheets(“mysheet”).name=”your sheet”
Atau kita ubah di properties window

ini hasilnya
Methods
Disamping properties, Object memiliki method, yaitu suatu
tindakan/action yang bisa dilakukan oleh obyek tersebut, misalnya Obyek
workbook memiliki method printout (untuk ngeprint), printpreview,
protect dll
misalnya Code untuk menutup/close suatu workbooks tanpa menyimpan perubahannya adalah sbb:
workbooks(“myfile.xlsx”).close false
Method close memiliki syntaxt sbb:
expression.Close(SaveChanges, Filename, RouteWorkbook)
expression A variable that represents a
Workbook object.
Parameters
| Name |
Required/Optional |
Data Type |
Description |
| SaveChanges |
Optional |
Variant |
If there are no changes to the workbook, this argument is ignored.
If there are changes to the workbook and the workbook appears in other
open windows, this argument is ignored. If there are changes to the
workbook but the workbook doesn’t appear in any other open windows, this
argument specifies whether changes should be saved. If set to True, changes are saved to the workbook. If there is not yet a file name associated with the workbook, then FileName is used. If Filename is omitted, the user is asked to supply a file name. |
| Filename |
Optional |
Variant |
Save changes under this file name. |
| RouteWorkbook |
Optional |
Variant |
If the workbook doesn’t need to be routed to the next recipient (if
it has no routing slip or has already been routed), this argument is
ignored. Otherwise, Microsoft Excel routes the workbook according to the
value of this parameter. If set to True, the workbook is sent to the next recipient. If set to False, the workbook is not sent. If omitted, the user is asked whether the workbook should be sent. |
Events
Event adalah suatu keadaan yang terjadi pada suatu Obyek, misalnya events pada obyek workbook adalah :
- Open
- Activate
- BeforeClose
- BeforeSave
- dan lain2 (lihat helpnya excel)
jadi misalnya pada event Open maka code yang terdapat pada event Open
akan dieksekusi, misalnya suatu file akan otomatic menampilkan
messagebox berisi tanggal dan waktu sekarang ,pada waktu file tsb
terbuka maka codenya adalah sbb:
Private Sub Workbook_Open()
MsgBox Date & vbCrLf & Time()
End Sub

caranya :
- Masuk ke VBE
- Klik kanan pada workbook kita di project explorer
- Pilih View code
- akan keluar code windows
- pilih workbook di isian sebelah kiri dan open pada isian sebelah kanan
- otomatic akan muncul sub prosedur baru yaitu :
- Private Sub Workbook_Open
- ketik code
- MsgBox Date & vbCrLf & Time()
- msgbox akan menampilkan suatu jendela message box
- vbCrLf akan membuat baris baru, sehingga text diatas akan terbagi menjadi 2 baris
- Date() dan Time() akan menampilkan tanggal dan waktu sekarang
- code diatas akan tereksekusi jika file ini dibuka
