+ Reply to Thread
Results 1 to 11 of 11

Thread: Deleting rows based on Conditional Formatting

  1. #1
    Registered User
    Join Date
    06-10-2009
    Location
    Montreal
    MS-Off Ver
    Excel 2003
    Posts
    10

    Deleting rows based on Conditional Formatting

    Hi,

    I'm very new to VBA programming for excel macros and I need some help. I'm trying to get Excel to delete entire rows based on the conditional formating i'm using.

    Basically, I want to get rid of all the rows that are of a certain color (let's say green, InteriorColor = 4). I've already come up with a way to delete rows based on color, but I have to take into account the Conditional formating i'm using.

    Here's what I already have:

    Option Explicit
    Sub DeleteGreenRows()
    Dim LR As Long, a As Long
    Application.ScreenUpdating = False
    LR = Cells(Rows.Count, 1).End(xlUp).Row
    For a = LR To 1 Step -1
      If Cells(a, 1).FormatConditions.InteriorColor = 4 Then
        Rows(a).EntireRow.Delete
      End If
    Next a
    Application.ScreenUpdating = True
    End Sub
    Any help would be awesome. Thanks!
    Last edited by Yobari; 06-11-2009 at 11:22 AM.

  2. #2
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    re: Deleting rows based on Conditional Formatting

    A better approach would be to put the conditional formatting formula in a separate column, and use that for both conditional formatting and as the test criteria for deletion.

    Please change your QUOTE tags to CODE tags.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    06-10-2009
    Location
    Montreal
    MS-Off Ver
    Excel 2003
    Posts
    10

    re: Deleting rows based on Conditional Formatting

    Alright, i didn't notice the CODE tags, sorry!

    I've included the file i'm using to test this macro on, it will obviously be used on a much bigger database export.

    I changed it so the conditional formating's function is in a separate column (which makes a lot of sense). But I'm a little confused as to what I should base the If Cells verification on or if it's a completely different way of doing it.
    Attached Files Attached Files

  4. #4
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    re: Deleting rows based on Conditional Formatting

    The conditional format would be Formula is =B1 or =NOT(B1), depending on what you want.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Registered User
    Join Date
    06-10-2009
    Location
    Montreal
    MS-Off Ver
    Excel 2003
    Posts
    10

    re: Deleting rows based on Conditional Formatting

    Yeah, the conditional formating wasn't really all that important, I was just looking at a way to identify the cells that are in both lists and then be able to delete them.

    What I'm asking about is how I can delete the rows where the value of the column B is FALSE (meaning that these values are in both lists).

    I thought I could use the code in the first post and modify it so that it would delete the rows where Cells(a,2) = "FALSE" but I seem to be missing something.

  6. #6
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    re: Deleting rows based on Conditional Formatting

    You can. Post the revised code.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  7. #7
    Registered User
    Join Date
    06-10-2009
    Location
    Montreal
    MS-Off Ver
    Excel 2003
    Posts
    10

    re: Deleting rows based on Conditional Formatting

    Option Explicit
    Sub DeleteFalseRows()
    Dim LR As Long, a As Long
    Application.ScreenUpdating = False
    LR = Cells(Rows.Count, 2).End(xlUp).Row
    For a = LR To 1 Step -1
      If Cells(a, 2) = "FALSE" Then
        Rows(a).EntireRow.Delete
      End If
    Next a
    Application.ScreenUpdating = True
    End Sub
    I know I'm missing something between Cells(a, 2) and the =

  8. #8
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    re: Deleting rows based on Conditional Formatting

    Sub DeleteFalseRows()
        Dim iRow    As Long
    
        With Application
            .ScreenUpdating = False
            .Calculation = xlCalculationManual
    
            For iRow = Cells(Rows.Count, 2).End(xlUp).Row To 1 Step -1
                If Cells(iRow, 2).Value = False Then Rows(iRow).Delete
            Next iRow
    
            .ScreenUpdating = True
            .Calculation = xlCalculationAutomatic
        End With
    End Sub
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  9. #9
    Registered User
    Join Date
    06-10-2009
    Location
    Montreal
    MS-Off Ver
    Excel 2003
    Posts
    10

    re: Deleting rows based on Conditional Formatting

    Awesome.

    Why are you changing the .Calculation to manual though? It works without it.

  10. #10
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    re: Deleting rows based on Conditional Formatting

    When you delete a row, Excel automatically recalculates. There no need for it, and it will slow things down.

    There is likewise no need to turn screenupdating off.

    Would you please mark the thread as Solved?
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  11. #11
    Registered User
    Join Date
    06-10-2009
    Location
    Montreal
    MS-Off Ver
    Excel 2003
    Posts
    10

    re: Deleting rows based on Conditional Formatting

    Yeah, I realized that when I tried it with a 28k row worksheet, you're better off turning it off.

    Thanks for everything!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0