+ Reply to Thread
Results 1 to 13 of 13

Delete row if cell is contained within a range

Hybrid View

  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

    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Set ws1 = Sheets("Sheet1")
    Set ws2 = Sheets("Sheet2")
    Dim iCtr As Long, lrow As Long
    Dim jCtr As Long
    Dim rng As Range
    
    irow = 2500
        Application.ScreenUpdating = False
        For iCtr = 2 To irow
        For jCtr = 2 To irow
    
          If ws1.Range("D" & iCtr) = ws2.Range("D" & jCtr) Then
          
        ws1.Rows(iCtr).Delete
         
    
    End If
            Next iCtr
            Next jCtr
            
    
    End Sub
    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.

    Sub ColumnSearch()
    
    Dim w1 As Worksheet, w2 As Worksheet, c As Range, fr As Long
    
    Application.ScreenUpdating = False
    
    Set w1 = Worksheets("sheet1")
    Set w2 = Worksheets("sheet2")
    
    For Each c In w1.Range("D2", Range("A" & Rows.Count).End(xlUp))
    
       If Len(c) Then
        fr = 0
        On Error Resume Next
         With w2
            fr = Application.Match(c, .Columns(4), 0)
            On Error GoTo 0
            If fr > 0 Then
            c.EntireRow.Delete
            End If
        End With
      End If
    Next c
    
    Application.ScreenUpdating = True
    Set w1 = Nothing
    Set w2 = Nothing
    
    End Sub

  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

    w1.Range("D2", Range("A" & Rows.Count).End(xlUp))
    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...
    For Each c In w1.Range("D2", Range("A" & Rows.Count).End(xlUp))

  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

    LR = w1.Range("A" & Rows.Count).End(xlUp).Row
    
    For Each c In w1.Range("D2:D" & LR)
    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?


    Sub ColumnSearch()
    
    Dim w1 As Worksheet, w2 As Worksheet, c As Range, fr As Long
    
    Application.ScreenUpdating = False
    
    Set w1 = Worksheets("sheet1")
    Set w2 = Worksheets("sheet2")
    
    For Each c In w1.Range("D2", Range("D" & Rows.Count).End(xlUp))
    
       If Len(c) Then
        fr = 0
        On Error Resume Next
         With w2
            fr = Application.Match(c, .Columns(4), 0)
            On Error GoTo 0
            If fr > 0 Then
            c.EntireRow.Delete
            End If
        End With
      End If
    Next c
    
    Application.ScreenUpdating = True
    Set w1 = Nothing
    Set w2 = Nothing
    
    End Sub

  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;

    c = fr(offset(0, 1))

  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.

    Sub Alter()
    
        Dim rsht1 As Long, rsht2 As Long
        
        rsht1 = Sheets("Sheet1").Range("A41")
        rsht2 = Sheets("Sheet2").Range("H" & Rows.Count).End(xlUp).Row
        
        For i = 4 To rsht1
            For j = 2 To rsht2
           
                If Sheets("Sheet1").Range("A" & i) = Sheets("Sheet2").Range("H" & j) Then
                
                Sheets("Sheet2").Range("I" & j).Copy
                Sheets("Sheet11").Rows(i).PasteSpecial x1Values
                    
                End If
            Next
        Next
             
    End Sub

  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