简体   繁体   中英

Override Conditional Formatting with Worksheet_SelectionChange

I have some code in a worksheet to highlight the row of a selected cell. However, this sheet has conditional formatting which colours some of the rows. The highlighting macro does not work on the rows where the formatting condition is being met (in the sense that the colour of the highlight is not overriding the colour 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 the conditional formatting? Eg. can I temporarily disable it for a selected row and re-enable it when the row is unselected?

The formatting is one rule applied to all cells. I figure in theory I could create an independent rule for every row (~500 of them) and then turn that off completely and later reapply it but that seems a little overboard.

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

Modify your Worksheet_SelectionChange to be closer to the following.

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 be constantly redefining a named range for each new selection of cells.

You didn't disclose what the CF rule actually was so I'm going to assume that is highlights cell that are not blank. Modify the existing CF rule to be of the Use a formula to determine which cells to format variety and adjust the following to suit your own CF rule then put it in the Format values where this formula is true: text box.

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

By adding a boolean criteria within an AND function and constantly redefining the mySelection range to the current selection you can override the CF rule's formatting.

条件格式覆盖

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM