VBA EXCEL突出显示除前两行之外的活动行 [英] VBA EXCEL Highlight active row except first two rows
问题描述
我有以下VBA CODE突出显示活动行。问题是这个代码会杀死前两行的背景颜色,而我希望前两行保持完整。这是我现在的代码:
I have the following VBA CODE to highlight an active row. The problem is that this code kills the background color of the first two rows, and I want the the first two rows remain completely intact. Here is my current code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Target.Row <> 1 And Target.Row <> 2 Then
Application.ScreenUpdating = False
Cells.Interior.ColorIndex = 0
Range("A1:AQ2").Interior.ColorIndex = 0
Target.EntireRow.Interior.ColorIndex = 8
Application.ScreenUpdating = True
End If
End Sub
推荐答案
Tim的代码应该已经解决了你的问题。在我的测试中,它的工作是完美的。
Tim's code should have solved your problem. It’s works perfectly in my test.
然而,这里是您原始代码的问题的解释:
Nevertheless, here is the explanation of the problems with your original code:
-
此行从所有行中删除内部颜色
This line is removing interior color from all rows
Cells.Interior.ColorIndex = 0
此行正在从列的行1和2删除内部颜色 A:Q
Range("A1:AQ2").Interior.ColorIndex = 0
所以这两行应该从你的原始代码:
Therefore these two lines should be deleted from your original code:
Cells.Interior.ColorIndex = 0
Range("A1:AQ2").Interior.ColorIndex = 0
此外,如果您打算删除工作表中任何先前的亮点,只留下新的然后添加一行:
Additionally, if your intention is to remove any prior highlights in the worksheet, leaving only the new one then add this line:
Range(Sh.Cells(3, 1), Sh.UsedRange.SpecialCells(xlLastCell)) _
.EntireRow.Interior.ColorIndex = xlColorIndexNone
这是您的原始代码调整:
This is your original code adjusted:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Target.Row <> 1 And Target.Row <> 2 Then
Application.EnableEvents = False
Application.ScreenUpdating = False
Range(Sh.Cells(3, 1), Sh.UsedRange.SpecialCells(xlLastCell)) _
.EntireRow.Interior.ColorIndex = xlColorIndexNone
Target.EntireRow.Interior.ColorIndex = 8
Application.ScreenUpdating = True
Application.EnableEvents = True
End If
End Sub
这篇关于VBA EXCEL突出显示除前两行之外的活动行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!