Excel宏突出显示所选行中的所有列

时间:2020-06-26 13:14:51

标签: excel vba

我的工作簿中有一个宏,该宏与热键相关,该热键突出显示当前所选行中的所有列。但是,它仅在选择了一行时才起作用。如果选择了多个,我想不出一种方法来调整它以突出显示所有行。这是我当前正在使用的代码。

Sub highlight_done()
'
' highlight_done Macro
'
' Keyboard Shortcut: Ctrl+q
'

Dim r As Long
    r = ActiveCell.Row
    
    Range("A" & r & ":Y" & r).Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 12611584
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    With Selection.Font
        .Color = vbWhite
        .TintAndShade = 0
    End With

End Sub

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

也许像下面这样,使用IntersectSelection.EntireRow来获取要着色的范围:

Sub highlight_done()
'
' highlight_done Macro
'
' Keyboard Shortcut: Ctrl+q
'

    If Not TypeOf Selection Is Range Then Exit Sub
    
    Dim rng As Range
    Set rng = Intersect(Selection.EntireRow, Range("A:Y"))
    
    With rng.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 12611584
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    With rng.Font
        .Color = vbWhite
        .TintAndShade = 0
    End With

End Sub