Excel VBA行突出显示 [英] Excel VBA Row Highlighting
问题描述
我找到了一些有关如何突出显示当前所选单元格行的示例.我的问题是,我只需要在第3行及更高行中执行此操作即可.
I've found some examples of how to highlight the row of the currently selected cell. My issue is that I need to do this only in rows 3 and higher.
这是我到目前为止所学到的:
This is what I picked up so far:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
Application.ScreenUpdating = False
Cells.Interior.ColorIndex = 0
With Target
.EntireRow.Interior.ColorIndex = 8
End With
Application.ScreenUpdating = True
End Sub
这可以做为广告,但是我正在努力如何不丢失行1和行2中标题单元格的背景色.我确定它需要某种"if",但是我不确定在哪里我需要把它放上.
This works as advertised, but I am struggling with how to not lose the background color I have for header cells in rows 1 and 2. I am sure it requires an "if" of some sort, but I am not sure where I need to put it.
此外,无论如何,我可以将其应用于整个工作簿吗?我的工作簿中有60张纸,如果我不能将代码复制60次,那将是理想的选择.
Also, is there anyway I can apply this to the entire workbook? I have 60 sheets in the workbook and if I can not replicate the code 60 times that would be ideal.
非常感谢您的帮助.
推荐答案
以下代码可以解决问题:
The following code will do the trick:
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:AF2").Interior.ColorIndex = 47
Target.EntireRow.Interior.ColorIndex = 8
Application.ScreenUpdating = True
End If
End Sub
您将其放置在 ThisWorkbook
中,而不是特定的 Sheet
中.
you place this in ThisWorkbook
instead of the specific Sheet
.
代码:
If Target.Row <> 1 and Target.Row <> 2 Then
检查 Target.Row
是否不等于 Row 1和2
这篇关于Excel VBA行突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!