+ Reply to Thread
Results 1 to 4 of 4

Macro to Delete Conditionally Formatted Cells

Hybrid View

  1. #1
    Registered User
    Join Date
    10-14-2008
    Location
    Ohio
    Posts
    3

    Macro to Delete Conditionally Formatted Cells

    The following macro goes throughout a sheet in a workbook and finds highlighted cells. When it finds a highlighted cell it clears the contents of that cell. It works well IF the highlighted cell is highlighted manually. However, I often use worksheets with conditional formatting that automatically finds cells with "bad" data and highlights them. The macro won't work for these conditionally formatted cells (which is terrible because these sheets are the reason I wrote the macro in the first place!). If anyone has any suggestions, you'd be a life saver!

    Here it is,

    Sub Clear_Contents_If_Highlighted()
    Dim rng As Range
    Set rng = Sheets("Sheet1").Range("A1:Z500")
    For Each cell In rng
    cell.Select
    If ActiveCell.Interior.ColorIndex = 6 Then
    cell.Clear
    End If
    Next
    End Sub
    Last edited by VBA Noob; 10-15-2008 at 08:54 AM.

  2. #2
    Forum Contributor
    Join Date
    05-21-2004
    Location
    UK
    Posts
    136
    Do you want to delete all CF cells, or just those cells where any of the CF conditions are TRUE?

  3. #3
    Registered User
    Join Date
    10-14-2008
    Location
    Ohio
    Posts
    3
    Quote Originally Posted by xld View Post
    Do you want to delete all CF cells, or just those cells where any of the CF conditions are TRUE?
    Just the ones where the CF is true!

  4. #4
    Forum Contributor
    Join Date
    05-21-2004
    Location
    UK
    Posts
    136
    Sub Clear_Contents_If_Highlighted()
    Dim rng As Range
    Dim cell As Range
        Set rng = Sheets("Sheet1").Range("A1:Z500")
        For Each cell In rng
            If IsCFMet(cell) Then cell.Clear
        Next
    End Sub
    
    '---------------------------------------------------------------------
    Public Function IsCFMet(rng As Range) As Boolean
    '---------------------------------------------------------------------
    Dim oFC As FormatCondition
    Dim sF1 As String
    Dim iRow As Long
    Dim iColumn As Long
    
       Set rng = rng(1, 1)
       If rng.FormatConditions.Count > 0 Then
          For Each oFC In rng.FormatConditions
             If oFC.Type = xlCellValue Then
                Select Case oFC.Operator
                   Case xlEqual
                      IsCFMet = rng.Value = oFC.Formula1
                   Case xlNotEqual
                      IsCFMet = rng.Value <> oFC.Formula1
                   Case xlGreater
                      IsCFMet = rng.Value > oFC.Formula1
                   Case xlGreaterEqual
                      IsCFMet = rng.Value >= oFC.Formula1
                   Case xlLess
                      IsCFMet = rng.Value < oFC.Formula1
                   Case xlLessEqual
                      IsCFMet = rng.Value <= oFC.Formula1
                      IsCFMet = (rng.Value >= oFC.Formula1 And _
                                 rng.Value <= oFC.Formula2)
                   Case xlNotBetween
                      IsCFMet = (rng.Value < oFC.Formula1 Or _
                                 rng.Value > oFC.Formula2)
                End Select
             Else
                 're-adjust the formula back to the formula that applies
                 'to the cell as relative formulae adjust to the activecell
                With Application
                   iRow = rng.Row
                   iColumn = rng.Column
                   sF1 = .Substitute(oFC.Formula1, "ROW()", iRow)
                   sF1 = .Substitute(sF1, "COLUMN()", iColumn)
                   sF1 = .ConvertFormula(sF1, xlA1, xlR1C1)
                   sF1 = .ConvertFormula(sF1, xlR1C1, xlA1, , rng)
                End With
                IsCFMet = rng.Parent.Evaluate(sF1)
             End If
             If IsCFMet Then Exit Function
         Next oFC
       End If 'rng.FormatConditions.Count > 0
    
    End Function

+ 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. Macro to Compare to Cells and Delete Row
    By randolphoralph in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-24-2008, 04:33 PM
  2. macro to delete one character in a cell
    By NewsyL in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-11-2007, 03:35 PM
  3. running macro deletes non designated cells
    By chrismcclain077 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-22-2007, 08:54 PM
  4. Using "mirror image" of cells to delete duplicate data?
    By abcd1234 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 06-11-2007, 09:12 AM
  5. Macro to hide columns based on contents of two cells
    By Korae13 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-18-2006, 12:40 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