+ Reply to Thread
Results 1 to 7 of 7

How to compare columns in 2 sheets then delete if no match

Hybrid View

  1. #1
    Registered User
    Join Date
    04-16-2015
    Location
    Phoenix, Arizona
    MS-Off Ver
    2007
    Posts
    9

    How to compare columns in 2 sheets then delete if no match

    Hello,
    1st Sheet "KEY"
    2nd sheet "Sheet3"

    1) Go through Col E in "Sheet3" (rows can vary in length) don't want to hardcode. Plus headers exist in row 1
    2) compare to "KEY" Col A. (no header in this sheet)
    3) If no match then delete row in "Sheet3"

    How do I go about writing the code?
    Just stumped how to go about this.

    Thanks,
    asu

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: How to compare columns in 2 sheets then delete if no match

    I assume column F of Sheet 3 is empty

    In which case this macro would help.

    
    Sub Test()
    
    LR = Cells(Rows.Count, 5).End(xlUp).Row
    
        Range("F2:F" & LR).FormulaR1C1 = "=IF(ISNA(MATCH(RC[-1],KEY!C[-5],0)),"""",1)"
        Range("F2:F" & LR).Value = Range("F2:F" & LR).Value
    
        ActiveWorkbook.Worksheets("Sheet3").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Sheet3").Sort.SortFields.Add Key:=Range("F2:F" & LR), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Sheet3").Sort
            .SetRange Range("E1:F" & LR)
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    NR = Range("F2:F" & LR).SpecialCells(xlCellTypeBlanks).Row
        Rows(NR & ":" & LR).Delete Shift:=xlUp
        Range("A1").Select
    columns(6).clear
    End Sub

    I added some error checking:-

    
    Sub Test()
    
    LR = Cells(Rows.Count, 5).End(xlUp).Row
    
        Range("F2:F" & LR).FormulaR1C1 = "=IF(ISNA(MATCH(RC[-1],KEY!C[-5],0)),"""",1)"
        Range("F2:F" & LR).Value = Range("F2:F" & LR).Value
    
        ActiveWorkbook.Worksheets("Sheet3").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Sheet3").Sort.SortFields.Add Key:=Range("F2:F" & LR), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Sheet3").Sort
            .SetRange Range("E1:F" & LR)
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        
    On Error Resume Next
    NR = Range("F2:F" & LR).SpecialCells(xlCellTypeBlanks).Row
        If Cells(NR, 6).Value = "" Then Rows(NR & ":" & LR).Delete Shift:=xlUp
    On Error GoTo 0
        Columns(6).Clear
        Range("A1").Select
    End Sub
    Last edited by mehmetcik; 08-25-2017 at 02:57 PM.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Registered User
    Join Date
    04-16-2015
    Location
    Phoenix, Arizona
    MS-Off Ver
    2007
    Posts
    9

    Re: How to compare columns in 2 sheets then delete if no match

    On "Sheet3" Col A-F are occupied with data.

    Since code is running through Col E I noticed it cleared contents of Col F.

    How do I keep COl F data intact?

    Looks like matching & deleting rows is working but need COL F to stay intact.

    Thanks,
    asu

  4. #4
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: How to compare columns in 2 sheets then delete if no match

    
    Sub Test()
    
    LR = Cells(Rows.Count, 5).End(xlUp).Row
    
        Range("G2:G" & LR).FormulaR1C1 = "=IF(ISNA(MATCH(RC[-2],KEY!C[-6],0)),"""",1)"
        Range("G2:G" & LR).Value = Range("G2:G" & LR).Value
    
        ActiveWorkbook.Worksheets("Sheet3").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Sheet3").Sort.SortFields.Add Key:=Range("G2:G" & LR), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Sheet3").Sort
            .SetRange Range("E1:G" & LR)
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        
    On Error Resume Next
    NR = Range("G2:G" & LR).SpecialCells(xlCellTypeBlanks).Row
        If Cells(NR, 7).Value = "" Then Rows(NR & ":" & LR).Delete Shift:=xlUp
    On Error GoTo 0
        Columns(7).Clear
        Range("A1").Select
    End Sub
    Last edited by mehmetcik; 08-25-2017 at 04:13 PM.

  5. #5
    Registered User
    Join Date
    04-16-2015
    Location
    Phoenix, Arizona
    MS-Off Ver
    2007
    Posts
    9

    Re: How to compare columns in 2 sheets then delete if no match

    Thank you so much!!

    I changed the [-5] to [-6]

    You saved me hours if not days trying to figure this out.

    Thanks,
    asu

  6. #6
    Registered User
    Join Date
    04-16-2015
    Location
    Phoenix, Arizona
    MS-Off Ver
    2007
    Posts
    9

    Re: How to compare columns in 2 sheets then delete if no match

    I just noticed that it was not matching all data.

    Is there a way I can attach sample file .xslm without exceeding size? My file exceeds the attachment size 240KB

    Thanks,
    asu

  7. #7
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,719

    Re: How to compare columns in 2 sheets then delete if no match

    Assumes that Column J in Sheet3 is empty.

    Sub Maybe()
    
    Dim lr As Long
    lr = Sheets("Key").Cells(Rows.Count, 1).End(xlUp).Row
    
    With Sheets("Sheet3")
    
        With .Range("E2:E" & .Cells(.Rows.Count, 5).End(xlUp).Row).Offset(, 5)
            .Formula = "=Countif(Key!R1C1:R" & lr & "C1,Sheet3!RC[-5])"
            .Value = .Value
        End With
        
        With .Range("J1:J" & .Cells(.Rows.Count, 10).End(xlUp).Row)
            .AutoFilter 1, 0
            .Offset(1).SpecialCells(12).EntireRow.Delete
        End With
        
        .AutoFilterMode = False
        .Columns("J:J").ClearContents
        
    End With
    
    End Sub

    If you want to attach a file that is too big, make it smaller!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Compare 2 columns, delete cells with no match
    By cmd in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 07-22-2019, 04:52 AM
  2. Default Compare 2 sheets and delete entire row if don't match
    By lapot in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-06-2016, 02:27 PM
  3. [SOLVED] Compare two columns in two sheets and delete if existed
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-30-2016, 06:30 AM
  4. Replies: 7
    Last Post: 04-18-2016, 02:26 AM
  5. Compare columns on 2 sheets and delete row based on format
    By tifferpat in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-16-2015, 04:37 PM
  6. Compare two columns from diff excel sheets and delete all non matching rows
    By Girija in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-24-2013, 11:08 PM
  7. Compare two sheets and delete row if match found
    By Steinwall in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-13-2011, 03:25 PM

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