Excel - Conditional Formatting - insert row

前端 未结 16 1493
轮回少年
轮回少年 2021-01-01 11:37

Using Offset or Indirect in \'Applies To\' does not seem to work. Is there any other way to stop conditional formatting from breaking after inserting row/s

相关标签:
16条回答
  • 2021-01-01 12:29

    Although this is quite old topic, my Excel sheets were also suffering from duplicating conditional formatting when inserting a new row.

    I was able to work around it. Let me share it with others, it might help too.

    In my case, all my conditional formatting rules were applied to the whole table. I realized that only certain rules are duplicated when inserting a new row. These rules are formula based comparing values in different rows.

    In my case, I wanted to render a horizontal border when values of two adjacent rows differ, e.g.:

    =$A2 <> $A1
    

    If I use OFFSET to refer to previous row, all is correct, no duplicated conditional formatting rules.

    =$A2<>OFFSET($A2; -1; 0)
    

    I actually put these conditional formatting formulas into a hidden column but the result should be the same.

    0 讨论(0)
  • 2021-01-01 12:29

    I agree with what has been posted previously; copy and paste values (or paste formulas) will totally work to not split up the conditional formatting.

    I'm a little lazy for that. And I don't want those who use my spreadsheets to have to do that. I'm also not confident that they will remember to do copy and paste values. :(

    I don't know if this solution will work for your needs, but I resorted to deleting all conditional formatting and reapplying the correct conditional formatting every time the workbook is opened.

    Because this macro runs every time the workbook is opened, the user does not need to change the way they copy and paste. They don't need to know that the macro is even there. They don't need to manually run the macro; it is automatic. I feel this creates a better user experience.

    Please keep in mind that this code needs to be copied and pasted into the "This Workbook" module; not a regular module.

    Private Sub Workbook_Open()
    'This will delete all conditional formatting and reapply the conditional formatting properly.
    'After copying and pasting the conditional formatting get split into two or more conditional formattings. After a few
    'weeks there are so many conditional formattings that Excel crashes and has to recover.
    
    Dim ws As Worksheet, starting_ws As Worksheet
    
    
    Set starting_ws = ActiveSheet   'remember which worksheet is active in the beginning
    Application.ScreenUpdating = False
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> "InvErr" Then
            ws.Activate
            Cells.FormatConditions.Delete
            ''Every Other Row Tan
            Range("A4:M203").FormatConditions.Add Type:=xlExpression, Formula1:="=ISODD(ROW(A4))"
            Range("A4:M203").FormatConditions(Range("A4:M203").FormatConditions.Count).SetFirstPriority
            Range("A4:M203").FormatConditions(1).Interior.PatternColorIndex = xlAutomatic
            Range("A4:M203").FormatConditions(1).Interior.ThemeColor = xlThemeColorDark2
            Range("A4:M203").FormatConditions(1).Interior.TintAndShade = 0
            Range("A4:M203").FormatConditions(1).StopIfTrue = False
    
            ''Highlight Duplicates Red
            Columns("B").FormatConditions.AddUniqueValues
            Columns("B").FormatConditions(Columns("B").FormatConditions.Count).SetFirstPriority
            Columns("B").FormatConditions(1).DupeUnique = xlDuplicate
            Columns("B").FormatConditions(1).Font.Color = -16383844
            Columns("B").FormatConditions(1).Font.TintAndShade = 0
            Columns("B").FormatConditions(1).Interior.PatternColorIndex = xlAutomatic
            Columns("B").FormatConditions(1).Interior.Color = 13551615
            Columns("B").FormatConditions(1).Interior.TintAndShade = 0
            Columns("B").FormatConditions(1).StopIfTrue = False
        End If
    Next
    
    starting_ws.Activate   'activate the worksheet that was originally active
    Application.ScreenUpdating = True
    
    End Sub
    
    0 讨论(0)
  • 2021-01-01 12:30

    This is a general problem with conditional formats. If you insert rows or shift things around, Excel assumes that you want the conditional format to shift with the cells, and that you want to insert cells with their original formatting (or none at all).

    So it tries its best to split up the formatted range according to the changes. Unfortunately "its best" is not very good. In lucky cases, your formatting rules get duplicated without you noticing; in unlucky cases they break for some or all of the applied range.

    This is especially a problem if you work with ListObjects (a.k.a. "Excel tables"). Insert some rows, reorder it a bit, drag some values around and the next time you look into your conditional formatting list, you have dozens to hundreds of duplicate rules. (example: http://blog.contextures.com/archives/2012/06/21/excel-2010-conditional-formatting-nightmare/)

    In my experience the quickest way to fix the mess is to delete all rules and recreate them (or not).

    Some sidenotes:

    • The applies-to range is always absolute. There is no way around that.
    • To make matters worse, conditional formats are treated like volatile formulas, meaning they are recalculated on lots of occasions (opening another file, scrolling around, etc). If you do not notice the split-ups, they can slow down the whole application significantly after a while.
    • If you go for VBA, you probably want to use the Worksheet_Calculate event, at least if your formulas refer to other worksheets (be aware of names!)
    0 讨论(0)
  • 2021-01-01 12:33

    I was having this problem while trying to create reports - once they're finished they don't need to change, but while I'm making them, I keep adding new lines and each new line mucks up the conditional formatting.

    This is by no means a good solution, but it was the best I could find without resorting to VBA - which was to:

    a) Make the conditional formatting rules apply to a whole column or more at a time

    for example instead of setting conditional formatting on C2 and C17, put an extra column, and write "this one" in rows 2 and 17, and then set the formatting for the whole of column C to be "if the other column says 'this one' then apply this format"

    b) Change the Applies To to be just $C$1:$C$2.

    c) Make changes and insert rows and stuff

    d) Then go back and change the Applies To to be $C:$C

    That way, while you change things and add things, the conditional formatting isn't there, but then you put it all back later.

    If, at a later date, you need to add a few more rows, first change it from $C:$C to $C$1:$C$2, then make the changes, and then put it back to $C:$C. That way you don't have to completely rewrite all the formatting rules from scratch as you would if you do what I've done previously which was just delete them all, curse, and start again ;)

    Obviously if you're planning on inserting rows up at the top in row 1 or 2, that won't work, but you could always set it to some other rows that you know you won't change.

    0 讨论(0)
提交回复
热议问题