+ Reply to Thread
Results 1 to 13 of 13

Delete row if cell is contained within a range

  1. #1
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Delete row if cell is contained within a range

    I want a macro to look at column D on sheet 1 from row 2 all the way down to the last used row and if for example column D row X is found in sheet2 column D then delete the entire row in sheet1.

  2. #2
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Delete row if cell is contained within a range

    I have the following which I think is along the right lines but doesn't work

    Please Login or Register  to view this content.
    Last edited by ScabbyDog; 03-06-2013 at 02:57 PM.

  3. #3
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Delete row if cell is contained within a range

    This is your code, you posted it a while back and I happened to comment on that thread. The match function is much faster than the "Find" function. I have tested it and it works.

    Please Login or Register  to view this content.

  4. #4
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Delete row if cell is contained within a range

    Ah yes didn't realise I could use that. Tested it but it seems to stop when a match is found so I need to run it about 15 times in a row for it to match and delete everything. Is there a smart way to loop the above? I just used For x = 1 to 15, Next x and it does the trick.
    Last edited by ScabbyDog; 03-06-2013 at 04:00 PM.

  5. #5
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Delete row if cell is contained within a range

    I think you need to change this in red from a to d

    Please Login or Register  to view this content.
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  6. #6
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Delete row if cell is contained within a range

    If there is data in column D whatever row it is there will definitely also be data in column A the same row. So would the above fix still apply Mike?

  7. #7
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Delete row if cell is contained within a range

    Well this is looping thru columns A:D. I thought you only wanted to look in column D. So changing A to D will speed up your code. Otherwise your Looping A2,B2,C2,D2,A3,B3 Etc...
    Please Login or Register  to view this content.

  8. #8
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Delete row if cell is contained within a range

    Sorry, It was my mistake, as Mike said you could use

    Please Login or Register  to view this content.
    This should loop through each cell in D2 all the way down as long as there are data in column A. You could use column D for LR as long as column A and D have the same length.

  9. #9
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Delete row if cell is contained within a range

    Last question,

    How can I alter it so that when a match is found instead of c.EntireRow.Delete it replaces that cell with the value of the cell directly to the right of where the match was found in sheet2?


    Please Login or Register  to view this content.

  10. #10
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Delete row if cell is contained within a range

    Would this be along the right lines;

    Please Login or Register  to view this content.

  11. #11
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Delete row if cell is contained within a range

    The above isn't quite working for me but I think I'm close. So if sheet 1 D5 for example matches sheet2 d55 then I want what is in sheet2 e55 pasted into sheet1 d5

  12. #12
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Delete row if cell is contained within a range

    Is the below any closer? It runs without error but doesn't do as intended.

    Please Login or Register  to view this content.

  13. #13
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Delete row if cell is contained within a range

    Solved it, just offsetting issues

+ 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