+ Reply to Thread
Results 1 to 6 of 6

Delete CF fill color, replace with regular fill color. Short code, can't figure it out!

Hybrid View

  1. #1
    Registered User
    Join Date
    01-11-2016
    Location
    USA
    MS-Off Ver
    2010
    Posts
    19

    Delete CF fill color, replace with regular fill color. Short code, can't figure it out!

    Dim cell As Range
    Set r12 = Range(r10.Offset(1), r10.End(xlDown))
    For Each cell In r12
    If ActiveCell.FormatConditions(1).Interior.Color = 49407 Then
       cell.FormatConditions.Delete
    End If
    Next
    That's the snippet of my code I'm working on (excluded the rest). It obviously doesn't include a line to fill the cell with a red color, so that would need to be added. Basically, I can't figure out how to go through each cell in the range r12, check if the conditional formatting color is 49407, and if it is then delete the formatting out of the cell and fill it with red.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,936

    Re: Delete CF fill color, replace with regular fill color. Short code, can't figure it out

    Dim cell As Range
    Set r12 = Range(r10.Offset(1), r10.End(xlDown))
    For Each cell In r12
         If cell.FormatConditions(1).Interior.Color = 49407 Then
               cell.FormatConditions.Delete
         End If
    Next cell
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    01-11-2016
    Location
    USA
    MS-Off Ver
    2010
    Posts
    19

    Re: Delete CF fill color, replace with regular fill color. Short code, can't figure it out

    Quote Originally Posted by protonLeah View Post
    Dim cell As Range
    Set r12 = Range(r10.Offset(1), r10.End(xlDown))
    For Each cell In r12
         If cell.FormatConditions(1).Interior.Color = 49407 Then
               cell.FormatConditions.Delete
         End If
    Next cell
    Thanks, and how do I fill the color if "red" the statement is true but only for those cells? I've tried it before and it fills the entire range rather than the few cells where the CF applied (aka it was filled 49407).

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,936

    Re: Delete CF fill color, replace with regular fill color. Short code, can't figure it out

    You don't provide enough info to add or modify format conditions via VBA but you can color fill the cell with:
    Option Explicit
    Sub bub()
        Dim cell As Range
        Set r12 = Range(r10.Offset(1), r10.End(xlDown))
        For Each cell In r12
             If cell.FormatConditions(1).Interior.Color = 49407 Then
                   cell.FormatConditions.Delete
                   cell.Interior.Color = vbRed
             End If
        Next cell
    End Sub

  5. #5
    Registered User
    Join Date
    01-11-2016
    Location
    USA
    MS-Off Ver
    2010
    Posts
    19

    Re: Delete CF fill color, replace with regular fill color. Short code, can't figure it out

    Doesn't seem to work, unfortunately. It's doing what my attempts did - colors all the cells. Here's the full code including your piece.
    Sub TestColor()
    
    Dim r10 As Range
    Dim r11 As Range
    
    Sheets("Sheet1").Cells.FormatConditions.Delete
    With ActiveWorkbook.Worksheets("Sheet1")
    Set r10 = .Range("A:A").Find(What:="Order Date", After:=.Range("A1"), LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)
    End With
    Set r11 = Range(r10.Offset(1), r10.End(xlDown).End(xlToRight))
    With r11
        .FormatConditions.Add Type:=xlTimePeriod, DateOperator:= _
            xlLastMonth
    '    .FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
        With .FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .Color = 49407
            .TintAndShade = 0
        End With
        .FormatConditions(1).StopIfTrue = False
    End With
    
    '''''''''''''''test area
        Dim cell As Range
        Set r12 = Range(r10.Offset(1), r10.End(xlDown))
        For Each cell In r12
             If cell.FormatConditions(1).Interior.Color = 49407 Then
                   cell.FormatConditions.Delete
                   cell.Interior.Color = vbRed
             End If
        Next cell
    '''''''''''''''''end test area
    
    End Sub
    To try, in column A enter in cells:
    A1 = Order Date
    A2 = 9/1/2016
    A3 = 10/1/2016
    A4 = 11/1/2016

    Ideally, when the code runs, it should conditional format just cell A3 (since it's the last month) in 49407 (and it does, if you edit out the test code), then in the test area for those conditionally formatted cells remove the coloring and just fill it with normal red.

    But like I said it does all cells

  6. #6
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,936

    Re: Delete CF fill color, replace with regular fill color. Short code, can't figure it out

    Maybe instead of deleting the old format, just modify it:
    Sub TestColor()
    
        Dim r10 As Range
        Dim r11 As Range
        
        With ActiveWorkbook.Worksheets("Sheet1")
        Set r10 = .Range("A:A").Find(What:="Order Date", After:=.Range("A1"), LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
        End With
        Set r11 = Range(r10.Offset(1), r10.End(xlDown).End(xlToRight))
        With r11
            With .FormatConditions(1).Interior
                .PatternColorIndex = xlAutomatic
                If .Color = 49407 Then
                    .Color = vbRed
                End If
                .TintAndShade = 0
            End With
        End With
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] How to fill color cells based on another cell's fill color?
    By putritersenyum in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 05-22-2016, 08:58 AM
  2. [SOLVED] Click oval option button to fill/unfill color, also change border color to match fill
    By Rocky2013 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-07-2015, 08:29 PM
  3. [SOLVED]Fill Color (highlight cells) but not to print fill color
    By itszille in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-24-2012, 05:37 PM
  4. Fill Cell with Color from Shape's color code
    By excelnewbie80 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-02-2012, 05:34 PM
  5. Delete any row WITHOUT a fill color
    By aorum in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-16-2011, 12:05 AM
  6. Delete any row WITH a red fill color (background)
    By aorum in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-13-2011, 07:52 PM
  7. Delete rows based on fill color
    By schueyisking in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-22-2008, 05:54 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1