Bước 1: Alt-F11 để mở Visual Editor
Bước 2: Chọn Workbook - Double click
Bước 3: chép đoạn code sau:
a) Trường hợp muốn Highlight Cell thực thi:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Application.ScreenUpdating = False
' Clear the color of all the cells
Cells.Interior.ColorIndex = 0
' Highlight the active cell - Thay doi tham so (8) de doi mau
Target.Interior.ColorIndex = 8
Application.ScreenUpdating = True
End Sub
b) Trường hợp muốn Highlight dòng và cột của Cell thực thi
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
Application.ScreenUpdating = False
' Clear the color of all the cells
Sh.Cells.Interior.ColorIndex = 0
With Target
' Highlight the entire row and column that contain the active cell
.EntireRow.Interior.ColorIndex = 8
.EntireColumn.Interior.ColorIndex = 8
End With
Application.ScreenUpdating = True
End Sub
Ghi chú: Có thể thay đổi tham số ColorIndex = 8 bằng các giá trị khác nếu muốn đổi màu.Trường hợp chỉ muốn thực hiện Highlight trên Sheet (không phải Workbook) thì Bước 3 chọn Sheet1, hay Sheet2... (mong muốn) và chép code tương tự. Tuy nhiên thay thế:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)Bằng
Private Sub Worksheet_SelectionChange(ByVal Target As Range)


No comments:
Post a Comment