Contoh VBA

Contoh VBA Excel untuk Pemula

Makro adalah teman terbaik Anda dalam hal meningkatkan produktivitas atau menghemat waktu di tempat kerja Anda. Mulai dari tugas kecil hingga tugas besar, kami dapat mengotomatiskan dengan menggunakan bahasa pengkodean VBA. Saya tahu seringkali Anda mungkin memikirkan beberapa batasan yang dimiliki excel tetapi dengan pengkodean VBA Anda dapat menghilangkan semua itu. Ok, jika Anda kesulitan dengan VBA dan masih pemula di artikel ini, kami akan memberikan beberapa contoh kode Makro VBA yang berguna di Excel.

Daftar 19 Contoh Teratas

  1. Cetak Semua Nama Lembar
  2. Masukkan Indeks Warna Berbeda di VBA
  3. Masukkan Nomor Seri Dari Atas
  4. Masukkan Nomor Seri Dari Bawah
  5. Masukkan Nomor Seri Dari 10 hingga 1
  6. Sisipkan Lembar Kerja sebanyak yang Anda inginkan
  7. Hapus Semua Lembar Kerja Kosong Dari Buku Kerja
  8. Sisipkan Baris Kosong Setelah Setiap Baris Lainnya
  9. Sorot Kesalahan Ejaan
  10. Ubah Semua Ke Karakter Huruf Besar
  11. Ubah Semua Ke Karakter Huruf Kecil
  12. Sorot Semua Sel yang Dikomentari
  13. Sorot Semua Sel Kosong
  14. Sembunyikan Semua Lembar Kecuali Satu Lembar
  15. Perlihatkan Semua Lembar
  16. Hapus Semua File di Folder
  17. Hapus Seluruh Folder
  18. Temukan Baris Terakhir Digunakan di Lembar
  19. Temukan Kolom yang Terakhir Digunakan di Lembar

Mari kita lihat masing-masing contoh ini secara detail.

Anda dapat mengunduh Template Excel Contoh VBA ini di sini - Template Excel Contoh VBA

# 1 - Cetak Semua Nama Lembar

Kode:

 Sub Print_Sheet_Names () Dim i As Integer For i = 1 To Sheets. Hitung Sel (i, 1). Value = Sheets (i) .Name Next i End Sub 

Ini akan mengekstrak semua nama lembar ke lembar aktif.

# 2 - Masukkan Indeks Warna Berbeda di VBA

Kode:

 Sub Insert_Different_Colours () Dim i As Integer For i = 1 To 56 Cells (i, 1]. Value = i Cells (i, 2) .Interior.ColorIndex = i Next End Sub 

Ini akan memasukkan angka dari 1 hingga 56 dan indeks warnanya di kolom berikutnya.

# 3 - Masukkan Nomor Seri Dari Atas

Kode:

 Sub Insert_Numbers_From_Top () Dim i As Integer For i = 1 To 10 Cells (i, 1]. Value = i Next i End Sub 

Ini akan memasukkan nomor seri dari 1 hingga 10 dari atas.

# 4 - Masukkan Nomor Seri Dari Bawah

Kode:

 Sub Insert_Numbers_From_Bottom () Dim i As Integer For i = 20 To 1 Step -1 Cells (i, 7). Value = i Next i End Sub 

Ini akan memasukkan nomor seri dari 1 hingga 20 dari bawah.

# 5 - Masukkan Nomor Seri Dari 10 hingga 1

Kode:

 Sub Ten_To_One () Dim i As Integer Dim j As Integer j = 10 For i = 1 To 10 Range ("A" & i). Nilai = jj = j - 1 Selanjutnya i End Sub 

Ini akan memasukkan nomor seri dari 10 hingga 1 dari atas.

# 6 - Sisipkan Lembar Kerja sebanyak yang Anda inginkan

Kode:

 Sub AddSheets () Dim ShtCount Sebagai Integer, i As Integer ShtCount = Application.InputBox ("Berapa Banyak Lembar yang ingin Anda sisipkan?", "Tambahkan Spreadsheet",,,,,, 1) Jika ShtCount = False Kemudian Keluar dari Sub Lain Untuk i = 1 Ke ShtCount Worksheets. Tambahkan Berikutnya i End If End Sub 

Ini akan meminta Anda untuk memasukkan jumlah lembar kerja yang ingin Anda sisipkan. Cukup tentukan nomornya di kotak input dan klik Ok, itu akan segera memasukkan banyak lembar itu.

# 7 - Hapus Semua Lembar Kerja Kosong Dari Buku Kerja

Kode:

 Sub Delete_Blank_Sheets () Dim ws As Worksheet Application.DisplayAlerts = False Application.ScreenUpdating = False Untuk Setiap ws Di ActiveWorkbook.Worksheets If WorksheetFunction.CountA (ws.UsedRange) = 0 Kemudian ws.Delete End If Next ws Application.DisplayAlerts = True Application .ScreenUpdating = True End Sub 

Ini akan menghapus semua lembar kerja kosong dari buku kerja yang sedang kita kerjakan.

# 8 - Sisipkan Baris Kosong Setelah Setiap Baris Lainnya

Kode:

 Sub Insert_Row_After_Every_Other_Row () Dim rng As Range Redup CountRow Sebagai Integer Dim i As Integer Set rng = Pilihan CountRow = rng.EntireRow.Count Untuk i = 1 Untuk CountRow ActiveCell.EntireRow.Insert ActiveCell.Offset (2, 0). Akhiri Sub 

Untuk yang pertama ini, Anda perlu memilih rentang tempat Anda ingin menyisipkan baris kosong alternatif.

# 9 - Sorot Kesalahan Ejaan

Kode:

 Sub Chech_Spelling_Mistake () Redupkan MySelection Sebagai Rentang Untuk Setiap MySelection Di ActiveSheet.UsedRange Jika Bukan Application.CheckSpelling (Word: = MySelection.Text) Lalu MySelection.Interior.Color = vbRed End Jika Berikutnya MySelection End Sub 

Pertama, pilih data dan jalankan kode VBA. Ini akan menyoroti sel yang memiliki kesalahan ejaan.

# 10 - Ubah Semua Menjadi Karakter Huruf Besar

Kode:

 Sub Change_All_To_UPPER_Case () Dim Rng As Range Untuk Setiap Rng Dalam Seleksi. Menelepon Jika Rng.HasFormula = False Then Rng.Value = UCase (Rng.Value) Akhiri Jika Rng Berikutnya Berakhir Sub 

Pertama, pilih data dan jalankan kodenya. Ini akan mengubah semua nilai teks menjadi karakter huruf besar.

# 11 - Ubah Semua Ke Karakter Huruf Kecil

Kode:

 Sub Change_All_To_LOWER_Case () Dim Rng Sebagai Range Untuk Setiap Rng Dalam Seleksi. Menelepon Jika Rng.HasFormula = False Then Rng.Value = LCase (Rng.Value) Akhiri Jika Rng Berikutnya Berakhir Sub 

First, select the data and run the code. It will convert all the text values to lower case characters in excel.

#12 – Highlight All the Commented Cells

Code:

 Sub HighlightCellsWithCommentsInActiveWorksheet() ActiveSheet.UsedRange.SpecialCells(xlCellTypeComments).Interior.ColorIndex = 4 End Sub 

Result: 

#13 – Highlight All the Blank Cells

Code:

 Sub Highlight_Blank_Cells() Dim DataSet As Range Set DataSet = Selection DataSet.Cells.SpecialCells(xlCellTypeBlanks).Interior.Color = vbGreen End Sub 

First, select the data range and run the code. It will highlight all the blank cells with green color.

#14 – Hide All Sheets Except One Sheet

Code:

 Sub Hide_All_Except_One() Dim Ws As Worksheet For Each Ws In ActiveWorkbook.Worksheets If Ws.Name  "Main Sheet" Then Ws.Visible = xlSheetVeryHidden Next Ws End Sub 

The above code hides all the sheets except the sheet named as “Main Sheet”. You can change the worksheet name as per your wish.

#15 – Unhide All Sheets

Code:

 Sub UnHide_All() Dim Ws As Worksheet For Each Ws In ActiveWorkbook.Worksheets Ws.Visible = xlSheetVisible Next Ws End Sub 

This will unhide all the hidden sheets.

#16 – Delete All Files in the Folder

Code:

 Sub Delete_All_Files() 'You can use this to delete all the files in the folder Test '' On Error Resume Next Kill "C:\Users\Admin_2.Dell-Pc\Desktop\Delete Folder\*.*" On Error GoTo 0 End Sub 

Change the folder path which is marked in red as per your folder deletion.

#17 – Delete Entire Folder

Code:

 Sub Delete_Whole_Folder() 'You can use this to delete entire folder On Error Resume Next Kill "C:\Users\Admin_2.Dell-Pc\Desktop\Delete Folder\*.*" 'Firstly it will delete all the files in the folder 'Then below code will delete the entire folder if it is empty RmDir "C:\Users\Admin_2.Dell-Pc\Desktop\Delete Folder\" 'Note: RmDir delete only a empty folder On Error GoTo 0 End Sub 

Change the folder path which is marked in red as per your folder deletion.

#18 – Find the Last Used Row in the Sheet

Code:

 Sub Last_Row() Dim LR As Long LR = Cells(Rows.Count, 1).End(xlUp).Row MsgBox LR End Sub 

Here we find the Last used Row in the Sheet

#19 – Find the Last Used Column in the Sheet

Code:

 Sub Last_Column() Dim LC As Long LC = Cells(1, Columns.Count).End(xlToLeft).Column MsgBox LC End Sub 

Here we find the Last used Column in the Sheet