Replace conditional formatting with Worksheet_SelectionChange

I have code on a worksheet to highlight the row of the selected cell. However, this sheet has conditional formatting that colors some lines. The selection macro does not work with lines in which the formatting condition is fulfilled (in the sense that the color of the selection does not override the color of the conditional formatting).

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

   Cells.Interior.ColorIndex = xlColorIndexNone 
    ActiveCell.EntireRow.Interior.ColorIndex = 19 'Highlight row
End Sub

Does anyone know a way around this without removing conditional formatting? For instance. can I temporarily disable it for the selected row and re-enable it when the row is not selected?

Formatting is one rule that applies to all cells. I suppose that theoretically I could create an independent rule for each row (~ 500 of them), and then completely disable it and then reapply it, but it seems a bit overboard.

+4
source share
1 answer

There is no need to use ActiveCell in the Worksheet_SelectionChange event macro. This is what Target is / does.

Modify your Worksheet_SelectionChange to be closer to the next.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Target.Name = "mySelection"
    Cells.Interior.Pattern = xlNone
    Target.EntireRow.Interior.ColorIndex = 19
End Sub

Now you will constantly redefine the named range for each new selection of cells.

, CF, , , . CF , , , CF, , : ​​ .

=AND(A1<>"", ROW(A1)<>ROW(mySelection))

AND mySelection , CF.

Conditional Formatting Override

+5

All Articles