Ví dụ về VBA - Danh sách 19 ví dụ VBA Excel hàng đầu cho người mới bắt đầu

Ví dụ về VBA trong Excel cho người mới bắt đầu

Macro là người bạn tốt nhất của bạn khi nói đến việc tăng năng suất của bạn hoặc tiết kiệm thời gian tại nơi làm việc của bạn. Ngay từ những nhiệm vụ nhỏ đến những tác vụ lớn, chúng ta có thể tự động hóa bằng cách sử dụng ngôn ngữ mã hóa VBA. Tôi biết đôi khi bạn có thể đã nghĩ đến một số hạn chế mà excel có nhưng với mã hóa VBA, bạn có thể loại bỏ tất cả những hạn chế đó. Ok, nếu bạn gặp khó khăn với VBA và vẫn là người mới bắt đầu trong bài viết này, chúng tôi sẽ đưa ra một số ví dụ hữu ích về mã VBA Macro trong Excel.

Danh sách 19 ví dụ hàng đầu

  1. In tất cả tên trang tính
  2. Chèn chỉ số màu khác nhau trong VBA
  3. Chèn số sê-ri từ trên cùng
  4. Chèn số sê-ri từ dưới cùng
  5. Chèn số sê-ri từ 10 đến 1
  6. Chèn các trang tính nhiều như bạn muốn
  7. Xóa tất cả các trang tính trống khỏi sổ làm việc
  8. Chèn hàng trống sau mỗi hàng khác
  9. Đánh dấu lỗi chính tả
  10. Thay đổi tất cả thành ký tự chữ hoa
  11. Thay đổi tất cả thành ký tự chữ thường
  12. Đánh dấu tất cả các ô đã nhận xét
  13. Đánh dấu tất cả các ô trống
  14. Ẩn tất cả các trang tính trừ một trang tính
  15. Hiện tất cả các trang tính
  16. Xóa tất cả các tệp trong thư mục
  17. Xóa toàn bộ thư mục
  18. Tìm hàng được sử dụng cuối cùng trong trang tính
  19. Tìm cột được sử dụng lần cuối trong trang tính

Hãy xem chi tiết từng ví dụ này.

# 1 - In Tất cả Tên Trang tính

Mã:

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

Thao tác này sẽ trích xuất tất cả các tên trang tính vào trang tính đang hoạt động.

# 2 - Chèn Chỉ số Màu khác nhau trong VBA

Mã:

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

Thao tác này sẽ chèn các số từ 1 đến 56 và chỉ số màu của chúng trong cột tiếp theo.

# 3 - Chèn số sê-ri từ trên cùng

Mã:

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

Thao tác này sẽ chèn các số sê-ri từ 1 đến 10 từ trên cùng.

# 4 - Chèn số sê-ri từ dưới cùng

Mã:

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

Thao tác này sẽ chèn các số sê-ri từ 1 đến 20 từ phía dưới.

# 5 - Chèn số sê-ri từ 10 đến 1

Mã:

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

Thao tác này sẽ chèn các số sê-ri từ 10 đến 1 từ trên cùng.

# 6 - Chèn Trang tính nhiều như bạn muốn

Mã:

Sub AddSheets () Dim ShtCount As Integer, i As Integer ShtCount = Application.InputBox ("Bạn muốn chèn bao nhiêu Sheets?", "Add Sheets",,,,,, 1) If ShtCount = False Then Exit Sub Else For i = 1 To ShtCount Worksheets. Thêm Tiếp theo Tôi Kết thúc Nếu Kết thúc Sub

Thao tác này sẽ yêu cầu bạn nhập số trang tính mà bạn muốn chèn. Chỉ cần xác định số trong ô nhập liệu và nhấp vào Ok, nó sẽ chèn nhiều sheet đó ngay lập tức.

# 7 - Xóa tất cả các trang tính trống khỏi sổ làm việc

Mã:

Sub Delete_Blank_Sheets () Dim ws As Worksheet Application.DisplayAlerts = False Application.ScreenUpdating = False For Each ws In ActiveWorkbook.Worksheets If WorksheetFunction.CountA (ws.UsedRange) = 0 Then ws.Delete End If Next ws Application.DisplayAlerts = True Application .ScreenUpdating = True End Sub

This will delete all the blank worksheets from the workbook we are working on.

#8 - Insert Blank Row After Every Other Row

Code:

Sub Insert_Row_After_Every_Other_Row() Dim rng As Range Dim CountRow As Integer Dim i As Integer Set rng = Selection CountRow = rng.EntireRow.Count For i = 1 To CountRow ActiveCell.EntireRow.Insert ActiveCell.Offset(2, 0).Select Next i End Sub

For this first, you need to select the range where you would like to insert alternative blank rows.

#9 - Highlight Spelling Mistake

Code:

Sub Chech_Spelling_Mistake() Dim MySelection As Range For Each MySelection In ActiveSheet.UsedRange If Not Application.CheckSpelling(Word:=MySelection.Text) Then MySelection.Interior.Color = vbRed End If Next MySelection End Sub

First, select the data and run the VBA code. It will highlight the cells which have spelling mistakes.

#10 - Change All To Upper Case Characters

Code:

Sub Change_All_To_UPPER_Case() Dim Rng As Range For Each Rng In Selection.Cells If Rng.HasFormula = False Then Rng.Value = UCase(Rng.Value) End If Next Rng End Sub

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

#11 - Change All To Lower Case Characters

Code:

Sub Change_All_To_LOWER_Case() Dim Rng As Range For Each Rng In Selection.Cells If Rng.HasFormula = False Then Rng.Value = LCase(Rng.Value) End If Next Rng End 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 “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:UsersAdmin_2.Dell-PcDesktopDelete 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:UsersAdmin_2.Dell-PcDesktopDelete 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:UsersAdmin_2.Dell-PcDesktopDelete 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

Ở đây chúng tôi tìm thấy Hàng được sử dụng lần cuối trong Trang tính

# 19 - Tìm Cột Được sử dụng Cuối cùng trong Trang tính

Mã:

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

Ở đây chúng tôi tìm thấy Cột được sử dụng lần cuối trong Trang tính

thú vị bài viết...