突出显示选定的行和列

时间:2021-02-27 14:21:36

标签: excel vba

我有下面的代码。突出显示始终启用。我试图找出如何创建一些我想要的东西来启用/禁用。 提前致谢。

  Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Static xRow
Static xColumn
If xColumn <> "" Then
    With Columns(xColumn).Interior
        .ColorIndex = xlNone
    End With
    With Rows(xRow).Interior
        .ColorIndex = xlNone
    End With
End If
pRow = Selection.Row
pColumn = Selection.Column
xRow = pRow
xColumn = pColumn
With Columns(pColumn).Interior
    .ColorIndex = 6
    .Pattern = xlSolid
End With
With Rows(pRow).Interior
    .ColorIndex = 6
    .Pattern = xlSolid
End With
End Sub

1 个答案:

答案 0 :(得分:2)

试试,

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    Cells.Interior.Color = xlNone
    If Range("a1") = "" Then Exit Sub 'Set operation according to a1 cell
    With Target
        .EntireRow.Interior.ColorIndex = 6
        .EntireColumn.Interior.ColorIndex = 6
    End With
End Sub

最好在工作表中插入 activeX 控件复选框并相应地设置行为。

enter image description here

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    Cells.Interior.Color = xlNone
    'If Range("a1") = "" Then Exit Sub
    If CheckBox1 Then Exit Sub
    With Target
        .EntireRow.Interior.ColorIndex = 6
        .EntireColumn.Interior.ColorIndex = 6
    End With
End Sub
相关问题