Results 1 to 10 of 83

Thread: Delete rows based on match criteria in two excel files or single Excel File

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0
    Code:
    Sub STEP3()
    Dim Wb1 As Workbook, Wb2 As Workbook
    Set Wb1 = Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\1.xls")
    Set Wb2 = Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\Hot Stocks\H2.xlsb")
    Dim Ws1 As Worksheet, Ws2 As Worksheet
    Set Ws1 = Wb1.Worksheets.Item(1)
    Set Ws2 = Wb2.Worksheets.Item(2)
    Dim Lr1 As Long, Lr2 As Long:
    Let Lr1 = Ws1.Range("A" & Ws1.Rows.Count & "").End(xlUp).Row
    Let Lr2 = Ws2.Range("A" & Ws2.Rows.Count & "").End(xlUp).Row
    Dim rngSrch As Range: Set rngSrch = Ws2.Range("A2:A" & Lr2 & "")
    Dim rngDta As Range: Set rngDta = Ws1.Range("B2:B" & Lr1 & "")
    
    Dim Cnt As Long
        For Cnt = Lr2 To 1 Step -1
        Dim MtchedCel As Variant
         Set MtchedCel = rngSrch.Find(what:=rngDta.Item(Cnt), After:=rngSrch.Item(1), LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlNext, MatchCase:=True)
            If Not MtchedCel Is Nothing Then
             
            Else
            rngDta.Rows(Cnt).EntireRow.Delete Shift:=xlUp
            End If
            
        Next Cnt
     Wb1.Close SaveChanges:=True
     Wb2.Close SaveChanges:=True
    End Sub

    I updated the macro Sir & i will use this macro
    & Now problem is solved Doc Sir
    any more updates & suggestions are there then plz let me know Doc Sir
    & Thnx For ur Great Suggestion & for helping me in solving this problem Sir

  2. #2
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10
    Quote Originally Posted by fixer View Post
    ....
    any more updates & suggestions are there then plz let me know...
    I would add comments. But that is just personal choice. Most people do not like to have comments.
    Code:
    Sub STEP3() '  https://excelfox.com/forum/showthread.php/2364-Delete-rows-based-on-match-criteria-in-two-excel-files?p=14587&viewfull=1#post14587    https://eileenslounge.com/viewtopic.php?f=30&t=34937
    Dim Wb1 As Workbook, Wb2 As Workbook
    Set Wb1 = Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\1.xls")
    Set Wb2 = Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\Hot Stocks\H2.xlsb")
    Dim Ws1 As Worksheet, Ws2 As Worksheet
    Set Ws1 = Wb1.Worksheets.Item(1)                                   ' First worksheet tab counting from the left
    Set Ws2 = Wb2.Worksheets.Item(2)                                   ' Second worksheet tab cunting from the left
    Dim Lr1 As Long, Lr2 As Long
    Let Lr1 = Ws1.Range("A" & Ws1.Rows.Count & "").End(xlUp).Row       ' Dynamically getting the last row in worksheet referenced by  Ws1
    Let Lr2 = Ws2.Range("A" & Ws2.Rows.Count & "").End(xlUp).Row       ' Dynamically getting the last row in worksheet referenced by  Ws2
    Dim rngSrch As Range: Set rngSrch = Ws2.Range("A2:A" & Lr2 & "")   ' The range that will be searched in
    Dim rngDta As Range: Set rngDta = Ws1.Range("B2:B" & Lr1 & "")     ' The range from which data will be looked for in rngSrch
    
    Dim Cnt As Long  '    For  each  rngDta.Item(Cnt)
        For Cnt = Lr2 To 1 Step -1                                     ' We take  -ve  steps = we go backwards. This is important when deleteing things.  See: https://excelfox.com/forum/showthread.php/2345-Appendix-Thread-(-Codes-for-other-Threads-HTML-Tables-etc-)?p=12902&viewfull=1#post12902
        Dim MtchedCel As Variant
         Set MtchedCel = rngSrch.Find(what:=rngDta.Item(Cnt), After:=rngSrch.Item(1), LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlNext, MatchCase:=True)
            If Not MtchedCel Is Nothing Then ' Range.Find  would return nothing if it did not find a match
                  ' If it was  Not  Nothing  then there was a match. So we do nothing
            Else  ' The attempt at a match failed, we got  Nothing  this is the condition to delete
             rngDta.Rows(Cnt).EntireRow.Delete Shift:=xlUp             ' The row is deleted , and so we have a space which is filled by shifting all rows in the worksheet  Up
            End If
            
        Next Cnt             ' Next  rngDta.Item(Cnt)
     Wb1.Close SaveChanges:=True                                       ' Save the file and close it
     Wb2.Close SaveChanges:=True                                       ' Save the file and close it
    End Sub
    Last edited by DocAElstein; 07-15-2020 at 03:26 AM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    KILL A MODERATOR!!

Similar Threads

  1. Replies: 29
    Last Post: 06-09-2020, 06:00 PM
  2. Replies: 3
    Last Post: 10-20-2015, 12:51 PM
  3. VBA To Delete Rows Based On Value Criteria In A Column
    By jffryjsphbyn in forum Excel Help
    Replies: 1
    Last Post: 08-15-2013, 12:45 PM
  4. Replies: 6
    Last Post: 08-14-2013, 04:25 PM
  5. Delete Remove Rows By Criteria VBA Excel
    By marreco in forum Excel Help
    Replies: 5
    Last Post: 12-20-2012, 05:56 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •