VBA单元格自适应高亮操作

1、单元格所在行和列高亮

第一种方式

Private Sub worksheet_selectionchange(ByVal target As Range)
    Cells.Interior.ColorIndex = xlColorIndexNone
    target.EntireRow.Interior.ColorIndex = 6
    target.EntireColumn.Interior.ColorIndex = 50
End Sub

第二种方式

Private Sub worksheet_selectionchange(ByVal target As Range)
    Dim rng As Range
    Cells.Interior.ColorIndex = xlNone
    Set rng = Application.Union(target.EntireRow, target.EntireColumn)
    rng.Interior.ColorIndex = 56
End Sub

2、展示所有颜色代码到单元格中

Sub 颜色()
    Dim i
    With Sheet2
        For i = 1 To 56
            .Cells(i, 1).Value = i
            .Cells(i, 1).Interior.ColorIndex = i
        Next
    End With
End Sub

 需注意:代码需在sheet中运行,运行区域高亮后不可使用复制粘贴等操作,以前单元格格式会被新格式被覆盖。

猜你喜欢

转载自www.cnblogs.com/hqczsh/p/11729562.html