+ Reply to Thread
Results 1 to 15 of 15

Delete Rows Based on Data in Separate Worksheet

Hybrid View

Smitty7 Delete Rows Based on Data in... 07-19-2012, 07:04 PM
davesexcel Re: Delete Rows Based on Data... 07-19-2012, 07:17 PM
xladept Re: Delete Rows Based on Data... 07-19-2012, 07:24 PM
Smitty7 Re: Delete Rows Based on Data... 07-21-2012, 05:00 PM
Trebor76 Re: Delete Rows Based on Data... 07-21-2012, 08:51 PM
xladept Re: Delete Rows Based on Data... 07-21-2012, 10:34 PM
Smitty7 Re: Delete Rows Based on Data... 07-25-2012, 02:01 PM
xladept Re: Delete Rows Based on Data... 07-25-2012, 02:50 PM
JosephP Re: Delete Rows Based on Data... 07-25-2012, 02:59 PM
Cutter Re: Delete Rows Based on Data... 07-25-2012, 09:58 PM
Trebor76 Re: Delete Rows Based on Data... 07-25-2012, 11:32 PM
JosephP Re: Delete Rows Based on Data... 07-26-2012, 01:48 AM
Trebor76 Re: Delete Rows Based on Data... 07-26-2012, 02:47 AM
xladept Re: Delete Rows Based on Data... 07-26-2012, 11:52 AM
Smitty7 Re: Delete Rows Based on Data... 07-27-2012, 05:48 PM
  1. #1
    Registered User
    Join Date
    07-16-2012
    Location
    Washington
    MS-Off Ver
    Excel 2003
    Posts
    6

    Exclamation Delete Rows Based on Data in Separate Worksheet

    Hi,

    I have a large list of data in a .txt file (LIST 1) that contains over 100,000 entries of website urls surrounded by a database reference number and special shortcodes like: (I removed actual url format due to not being able to post)

    18967[abs]URL1[abs]wp[abs]0[abs]active
    18968[abs]URL2[abs]wp[abs]0[abs]active
    18969[abs]URL3[abs]wp[abs]0[abs]active

    I will have to split it up into at least 2 worksheets due to the large number of entries.

    I have a small list of urls (LIST 2) without the special code or database numbers in a column of another worksheet such as:

    URL1
    URL3

    I want to delete all rows from my LIST 1 that contain urls from LIST 2.

    Thanks!

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,524

    Re: Delete Rows Based on Data in Separate Worksheet

    To Attach a File:

    1. Scroll down to the window below your post Additional Options
    2. In the frame Attach Files you will see the button Manage Attachments
    3. Click the button.
    4. A new window will open titled Manage Attachments - Excel Forum.
    5. Click the Browse... button to locate your file for uploading.
    6. This will open a new window File Upload.
    7. Once you have located the file to upload click the Open button. This window will close.
    8. You are now back in the Manage Attachments - Excel Forum window.
    9. Click the Upload button and wait until the file has uploaded.
    10. Close the window and then click Submit.

    http://www.excelforum.com/album.php?...hmentid=149278

  3. #3
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Delete Rows Based on Data in Separate Worksheet

    Hi Smitty7,

    Perhaps:

    Sub DelURL(): Dim List1 As Worksheet, List2 As Worksheet
    Dim e1 As Long, e2 As Long, i As Long, j As Long
    Set List1 = Worksheets("List1"): Set List2 = Worksheets("List2")
    e2 = List2.Range("A" & Rows.Count).End(xlUp).Row
    e1 = List1.Range("A" & Rows.Count).End(xlUp).Row
    For i = i To e2
    For j = 1 To e1
    DumpedURL:
    If InStr(1, List2.Cells(j, 1), List1.Cells(i, 1)) Then
    List2.Cells(j, 1).EntireRow.Delete Shift:=xlUp
    GoTo DumpedURL: e1 = e1 - 1: End If
    Next j
    Next i
    End Sub
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  4. #4
    Registered User
    Join Date
    07-16-2012
    Location
    Washington
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Delete Rows Based on Data in Separate Worksheet

    @xladept - I wasn't able to get it to work. I have attached an .xls file with the first couple thousand entries to be cleaned in Sheet1 and the reference urls in Sheet 2.

    So, I would like any row in Sheet1 containing a url from Sheet2 to be deleted. I don't want the urls in Sheet2 to be deleted.

    Thanks!
    Attached Files Attached Files

  5. #5
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Delete Rows Based on Data in Separate Worksheet

    Hi Smitty7,

    Try this while on the tab to have the items deleted, though initially from a copy of data as the results cannot be undone if they're not as expected:

    Option Explicit
    Sub Macro1()
    
        'http://www.excelforum.com/excel-programming/846630-delete-rows-based-on-data-in-separate-worksheet.html
        
        Dim lngMyCol As Long, _
            lngMyRow As Long
            
        lngMyCol = Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
        lngMyRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            
        Application.ScreenUpdating = False
        
        With Columns(lngMyCol)
            With Range(Cells(1, lngMyCol), Cells(lngMyRow, lngMyCol))
                .Formula = "=IF(ISERROR(VLOOKUP(LEFT(MID(A1,SEARCH(""http"",A1),255),SEARCH(""["",MID(A1,SEARCH(""http"",A1)+1,255))),Sheet2!A:A,1,FALSE)),"""",""DEL"")"
                .Value = .Value
            End With
            .Replace "DEL", "#N/A", xlWhole
            On Error Resume Next 'Turn error reporting off - OK to ignore 'No cells found' message
                .SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Delete
            On Error GoTo 0 'Turn error reporting back on
            .Delete
        End With
        
        Application.ScreenUpdating = True
    
    End Sub
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Delete Rows Based on Data in Separate Worksheet

    Hi Smitty7,

    It's fixed but a trifle slow - it leaves you with 8121 rows in Sheet1:

    Sub DelURL(): Dim List1 As Worksheet, List2 As Worksheet
    Dim e1 As Long, e2 As Long, i As Long, j As Long
    Set List1 = Worksheets("Sheet1"): Set List2 = Worksheets("Sheet2")
    e2 = List2.Range("A" & Rows.Count).End(xlUp).Row
    e1 = List1.Range("A" & Rows.Count).End(xlUp).Row
    For i = 1 To e2
    For j = 1 To e1
    DumpedURL:
    If InStr(1, List1.Cells(j, 1), List2.Cells(i, 1)) Then
    List1.Cells(j, 1).EntireRow.Delete Shift:=xlUp
    e1 = e1 - 1: GoTo DumpedURL: End If
    Next j
    Next i
    End Sub

  7. #7
    Registered User
    Join Date
    07-16-2012
    Location
    Washington
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Delete Rows Based on Data in Separate Worksheet

    @xladept and @Trebor76 - Thanks! Both of those solutions worked well!

  8. #8
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Delete Rows Based on Data in Separate Worksheet

    You're welcome!

  9. #9
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Delete Rows Based on Data in Separate Worksheet

    fyi it would likely be a lot faster to create a range of all the matching cells using Union and then delete that range in one hit at the end instead of deleting one row at a time.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  10. #10
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Delete Rows Based on Data in Separate Worksheet

    @ Smitty7

    Based on your last post it seems that you are satisfied with the solution(s) you've received but you haven't marked your thread as SOLVED. I'll do that for you now but please keep in mind for your future threads that Rule #9 requires you to do that yourself. If your problem has not been solved you can use Thread Tools (located above your first post) and choose "Mark this thread as unsolved".
    Thanks.

    Also, as a new member of the forum, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

  11. #11
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Delete Rows Based on Data in Separate Worksheet

    fyi it would likely be a lot faster to create a range of all the matching cells using Union and then delete that range in one hit at the end instead of deleting one row at a time
    My code deletes an entire block in one go?

  12. #12
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Delete Rows Based on Data in Separate Worksheet

    yes it does. xladept's code doesn't though-I should have made clear to whom I was addressing the comment.

  13. #13
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Delete Rows Based on Data in Separate Worksheet

    Oh OK, I see. Thanks for the clarification.

  14. #14
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Delete Rows Based on Data in Separate Worksheet

    I didn't realize that block deletions were appropriate to the data - I'll look harder in the future.

  15. #15
    Registered User
    Join Date
    07-16-2012
    Location
    Washington
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Delete Rows Based on Data in Separate Worksheet

    @Trebor76 - I did notice that your code was MUCH quicker. I'm such a noob to VBA that I didn't know why

    @Cutter - Thanks for marking the thread as solved (will remember next time.) Just to let you know I did add reputation to both Trebor76 and xladept.

    Thanks!

+ 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.6.0 RC 1