Next macro version to answer this Thread Post:
https://excelfox.com/forum/showthrea...ll=1#post14635
Code:Sub STEP3b() ' 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(ThisWorkbook.Path & "\1(sample).xls") ' Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\1.xls") Set Wb2 = Workbooks.Open(ThisWorkbook.Path & "\H2(SAMPLE).xlsx") ' 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("B1:B" & Lr1 & "") ' The range from which data will be looked for in rngSrch Dim Cnt As Long ' For each rngDta.Item(Cnt) For Cnt = Lr1 To 2 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
So finally what this macro is doing. In English:
Data values in Ws1 , (first worksheet in "1(sample).xls") column B , are looked for ( attempted to be matched ) to the column A range in Ws2 ( second worksheet in "H2(SAMPLE).xlsx")
If a match is found, then nothing is done. If a match was not found, then the entire row containing the data value in Ws1 is deleted




Reply With Quote
Bookmarks