Consulting

Results 1 to 13 of 13

Thread: VBA to delete rows if font colour is Red?

  1. #1

    VBA to delete rows if font colour is Red?

    Hi there,

    I'm trying to create a macro which will search through all data in A2-Q2 down and delete any rows where a cell contains red font. This may occur in any column between A-Q. I can only get this to work for a set range but my range will change every day as there may be more rows or less rows? - (for example, in below code, this searches to Q3000 but I need this to change in case I have more or less rows). Can anyone help with this at all?

    Sub DeleteRedCells()
    
    Dim rng As Range
    
    Set rng = [A2:Q3000]
    For Each Cell In rng
     If Cell.Font.ColorIndex = 3 Then
      Cell.ClearContents
     End If
    Next Cell
    
    End Sub

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    deleting rows (the title of the thread) is different than clearing cells (the code you posted).
    ?
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    Oh Apologies!! It is delete row I want to do, not clear contents, sorry

  4. #4
    Try this code:

    Sub RemoveRedFont()
    Dim k As Long
    With ActiveSheet
    If .AutoFilterMode Then .AutoFilterMode = False
    k = .Range("A" & .Rows.Count).End(xlUp).Row
    With .Range("A1").Resize(k)
    .AutoFilter
    .AutoFilter Field:=1, Criteria1:=RGB(255, 0, 0), Operator:=xlFilterFontColor
    .Offset(1).Resize(k - 1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    End With
    .AutoFilterMode = False
    End With
    End Sub

  5. #5
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    Sub vbax_53658_DelRowsOnCondition()
    
        Dim LastRow As Long, LastCol As Long
        Dim r As Long, c As Long
        
        With Worksheets("Sheet1") 'change worksheet name to suit
            LastRow = .Cells.Find("*", , , , xlByRows, xlPrevious).Row
            LastCol = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
            For r = LastRow To 2 Step -1
                For c = 1 To LastCol
                    If .Cells(r, c).Font.ColorIndex = 3 Then
                        .Rows(r).Delete
                        Exit For
                    End If
                Next c
            Next r
        End With
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  6. #6
    Apologies, I can see after testing your above code (mancubus) on a different spreadsheet with normal cells changed to red font - this does work. This will not work on my spreadsheet however, as I have used conditional formatting to make the fonts red. Is there still a way to do this at all or will it not be possible due to CF? Sorry, I had not realised this

  7. #7
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    insert DisplayFormat after cell reference

    Sub vbax_53658_DelRowsOnCondition_CF()
         
        Dim LastRow As Long, LastCol As Long
        Dim r As Long, c As Long
         
        With Worksheets("Sheet1") 'change worksheet name to suit
            LastRow = .Cells.Find("*", , , , xlByRows, xlPrevious).Row
            LastCol = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
            For r = LastRow To 2 Step -1
                For c = 1 To LastCol
                    If .Cells(r, c).DisplayFormat.Font.ColorIndex = 3 Then
                        .Rows(r).Delete
                        Exit For
                    End If
                Next c
            Next r
        End With
         
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  8. #8
    Wow this really worked! Thank you so much, I honestly didn't believe I'd find a solution!!

    There's only one slight problem and that is that I have a button on my spreadsheet which is what I click to run this macro, however it gets deleted in the process. Is there any way of keeping it there?



    ^^EDIT: I have found how to fix this now. Many thanks for all your help, it's super appreciated; you're a genius ^_^

  9. #9
    Would there be a way to adjust this so that it only KEEPS rows where the contents of column D is conditionally formatted red?

    Also, at the moment this macro is taking a veeery long time to run as I have a lot of data to work through. Is there any way of speeding it up at all or is this just the way it has to be?

  10. #10
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    this will delete the rows if the cells in Column D have Automatic Font Color (default, unchanged font color)

    Sub vbax_53658_Delete_Not_CF_Rows()
         
        With Worksheets("Sheet1") 'change worksheet name to suit
            .AutoFilterMode = False
            .UsedRange.AutoFilter Field:=4, Operator:=xlFilterAutomaticFontColor
            .UsedRange.Columns(1).Offset(1).SpecialCells(12).EntireRow.Delete
            .AutoFilterMode = False
        End With
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  11. #11
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    and this will speed up the row deletion process.

    be very careful: it loops columns, filters rows with red font (whether they be conditionally formatted or not), and deletes them.

    Sub vbax_53658_Delete_Red_Font_Rows_CF_Or_Colored()
         
        Dim LastCol As Long, c As Long
        
        With Worksheets("Sheet1") 'change worksheet name to suit
            .AutoFilterMode = False
            LastCol = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
            For c = 1 To LastCol
                .UsedRange.AutoFilter Field:=c, Criteria1:=RGB(255, 0, 0), Operator:=xlFilterFontColor
                .UsedRange.Columns(1).Offset(1).SpecialCells(12).EntireRow.Delete
                .AutoFilterMode = False
            Next c
        End With
    
    End Sub
    Last edited by mancubus; 10-30-2015 at 07:49 AM. Reason: typo
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  12. #12
    Perfect answers, thank you!

  13. #13
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you are welcome. thanks for marking the thread as solved.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Tags for this Thread

Posting Permissions

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