Consulting

Results 1 to 7 of 7

Thread: Solved: Delete Rows if cells in columns are empty

  1. #1
    VBAX Contributor
    Joined
    Mar 2009
    Location
    Indiana
    Posts
    113
    Location

    Question Solved: Delete Rows if cells in columns are empty

    Hello,

    I have a worksheet that imports a table from access and have conditional fomatting that Identifies whether items are late are not. If it is late to department1, it turns the cell in AC red. If it is late to department1, it turns the cell red in AD. I would like the code to find the last row in the workbook, which there will always be something in column D...so you could base the last row off of that column. Anyways, I want each row that doesnt have coloring in columns AC and AD to be deleted from the workbook. I only want Rows that have columns that are red in AC and AD, or both at the same time. Any help would be appreciated.

    Thanks,
    John

  2. #2
    VBAX Mentor
    Joined
    Jun 2004
    Posts
    363
    Location
    You need a macro that will test the two conditions that cause the color changes, and if both are false, then delete the row.

    Something like the following. It assumes column 1 has data to the end of the table. It then loops the table and checks each row to see if the value in column 2 is greater than the value in column 3 and if the value in column 4 is greater than the value in column 5. If both conditions are true, the row is deleted.

    [vba]Sub deleter()

    Dim i As Long, lRow As Long
    lRow = Cells(Rows.Count, 1).End(xlUp).Row

    For i = lRow To 2 Step -1
    If Cells(i, 2) > Cells(i, 3) And Cells(i, 4) > Cells(i, 5) Then
    Cells(i, 1).EntireRow.Delete
    End If
    Next

    End Sub[/vba]

  3. #3
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    It's a little more difficult than that as you have to test for the conditional formatting and evaluate the formula.

    Can you provide a sample workbook with the conditional formatting in place?
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  4. #4
    VBAX Mentor
    Joined
    Jun 2004
    Posts
    363
    Location
    Quote Originally Posted by Simon Lloyd
    I.... you have to test for the conditional formatting and evaluate the formula.
    That's what this statement was intended to convey " You need a macro that will test the two conditions that cause the color changes"

  5. #5
    VBAX Contributor
    Joined
    Mar 2009
    Location
    Indiana
    Posts
    113
    Location

    sample workbook

    Hello,

    I have attached a sample workbook so that you can see what Im working with here. Any help would be appreciated. I scrubbed the document to protect myself. If you have any other questions or comments, let me know.

    Thanks,
    John

  6. #6
    VBAX Mentor
    Joined
    Jun 2004
    Posts
    363
    Location
    I believe this one should do it:
    [VBA]
    Sub deleter()
    Application.ScreenUpdating = False
    Dim i As Long, lRow As Long
    lRow = Cells(Rows.Count, 4).End(xlUp).Row

    For i = lRow To 2 Step -1
    If Not ((Cells(i, 18) <> "" And Cells(i, 18) < Date And Cells(i, 19) = "" And Cells(i, 21) = "") Or _
    (Cells(i, 22) = "" And Cells(i, 20) < Date And Cells(i, 20) <> "")) Then
    Cells(i, 1).EntireRow.Delete
    End If
    Next
    Application.ScreenUpdating = True
    End Sub

    [/VBA]

  7. #7
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    Hi John,

    How are you getting the data from Access? Can I suggest that you setup a query in Access and import that rather than the whole table? That way you're only importing the records that you actually want, instead of importing everything and deleting the records you don't want.

Posting Permissions

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