how to keep initial cell color, while code is hilighting active row

前端 未结 2 1939
刺人心
刺人心 2021-01-23 13:02

I have this code for workbook:

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
\'toggles worksheet colors
\'code will remove         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-23 13:48

    Here's an alternate approach which makes use of the fact that Excel always "overlays" Conditional Formatting on top of whatever formatting is already on the sheet.

    Define a worksheet-level name "ROWNUM" and assign a value of 0.

    Add a conditional format using the formula =(ROW()=ROWNUM) and add whatever formatting you want to use for row highlighting.

    Your SelectionChange sub is then just:

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
         Me.Names("ROWNUM").RefersToR1C1 = "=" & Target.Cells(1).Row
    End Sub
    

提交回复
热议问题