簡體   English   中英

Excel VBA行突出顯示

[英]Excel VBA Row Highlighting

我找到了一些有關如何突出顯示當前所選單元格行的示例。 我的問題是,我只需要在第3行及更高行中執行此操作。

到目前為止,這是我得到的:

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”,但是我不確定在哪里把它。

此外,無論如何,我可以將其應用於整個工作簿嗎? 我的工作簿中有60張紙,如果我不能將代碼復制60次,那將是理想的選擇。

任何幫助是極大的贊賞。

以下代碼可以解決問題:

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

編碼:

If Target.Row <> 1 and Target.Row <> 2 Then

檢查Target.Row是否等於Row 1 and 2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM