+ Reply to Thread
Results 1 to 8 of 8

Delete rows based on list

  1. #1
    Registered User
    Join Date
    10-12-2007
    Posts
    36

    Delete rows based on list

    Guys, I have a simple VBA question but I've been looking around and I can't crack it!

    I'd like to delete rows based on a list. So:


    I have a tab with a big list of data:
    A B C D
    --------
    XX AA 3 4
    YY BB 5 8
    ZZ CC 6 9
    ...

    Now I'd like to delete rows based on another list on another tab. This list could expand in the future.

    So the other tab (delete) list:
    A C
    ----
    XX 3
    ZZ 6

    So the macro would check A&C from delete list = A&C from original list and delete the row. So the result on first tab would be:

    A B C D
    --------
    YY BB 5 8


    Thank you so much.
    DrChris

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Delete rows based on list

    Hi,

    Assumptions.
    Data is on sheet1 A1:D4 with ABCD as column labels on row 1
    The delete list is on sheet2 in A1:C3 with labels on row 1.

    First create two range names
    Data
    =OFFSET(Sheet1!$A$1,0,0,COUNTA(Sheet1!$A:$A),4)
    DeleteCriteria
    =OFFSET(Sheet2!$A$1,0,0,COUNTA(Sheet2!$A:$A),2)

    Then add the following macro

    Please Login or Register  to view this content.
    HTH
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    10-12-2007
    Posts
    36

    Re: Delete rows based on list

    Thank u for your help
    Chris
    Last edited by drchris; 10-19-2009 at 09:34 AM.

  4. #4
    Registered User
    Join Date
    10-12-2007
    Posts
    36

    Re: Delete rows based on list

    Rich it doesnt work.

    After I run the sub, it hides cells 2-4
    And I get a run time error: No cells were found.

    Thank u
    DrChris
    Last edited by drchris; 10-19-2009 at 10:13 AM.

  5. #5
    Registered User
    Join Date
    10-12-2007
    Posts
    36

    Re: Delete rows based on list

    Book is attached. I would really appreciate someones help as its for work and my boss is getting on my nerves to get it to work.
    Thanks
    Chris
    Attached Files Attached Files

  6. #6
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Delete rows based on list

    Hi,

    See the attached. It works in this book.
    (I've changed the last instruction from a filter to a showall, but that would not have caused the problem you flagged up).

    Regards
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    10-12-2007
    Posts
    36

    Re: Delete rows based on list

    You're right. It works fine now, so very appreciated for that Rich.
    I need to adjust it to work on my work spreadsheet now.

    So instead of looking at columns 1 and 3, it needs to look at 4 & 6. And there are 14 columns in sheet1 not just 4.

    Ive changed this to 14
    Data
    =OFFSET(Sheet1!$A$1,0,0,COUNTA(Sheet1!$A:$A),14)

    But also the code to 14

    Sub DeleteRows()
    Range("Data").AdvancedFilter action:=xlFilterInPlace, criteriarange:=Range("deletecriteria")
    Range("Data").Offset(1, 0).Resize(Range("Data").Rows.Count - 1, 14).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    Sheet1.ShowAllData
    End Sub

    But no luck. Any ideas?

    Thank you
    C

  8. #8
    Registered User
    Join Date
    10-12-2007
    Posts
    36

    Re: Delete rows based on list

    Got it to work now. Code didnt need change it seems.
    Thank you Rich.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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