Results 1 to 10 of 83

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #11
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10
    Before:- ( from your uploaded files )
    _____ Workbook: sample1.xlsx ( Using Excel 2007 32 bit )
    Row\Col
    A
    B
    C
    D
    1
    2
    ACC
    150
    3
    DLF
    120
    4
    5
    match column A of sample1.xlsx with column B of sample2.xlsx
    6
    If it matches then do nohing
    7
    if it doesn’t matches then delete that enire row
    8
    Worksheet: Sheet1

    _____ Workbook: sample2.xlsx ( Using Excel 2007 32 bit )
    Row\Col
    A
    B
    C
    D
    E
    F
    G
    H
    I
    1
    Exchange Symbol Series/Expiry Open High Low Prev Close LTP
    2
    NSE ACC EQ
    1034
    1068.75
    988
    998.45
    998.45
    3
    NSE ADANIENT EQ
    123.05
    135
    118.2
    120.9
    120.9
    This entire row will be deleted
    4
    NSE DLF EQ
    235
    235.8
    203
    207.8
    207.8
    5
    Worksheet: Sheet1

    Now I run this

    Code:
    Sub conditionally_delete()
    Dim Wb1 As Workbook, Wb2 As Workbook, Ws1 As Worksheet, Ws2 As Worksheet
     Set Wb1 = Workbooks("sample1.xlsx") ' Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\Upstox\STEP1U.xlsb")
     Set Ws1 = Wb1.Worksheets.Item("Sheet1") ' worksheet with string tab name of  Sheet1
     Set Wb2 = Workbooks("sample2.xlsx") '  Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\1.xls")
     Set Ws2 = Wb2.Worksheets.Item(1) ' worksheet of first tab
    '1b Ranges
    Dim Rng1 As Range, Rng2 As Range
     Set Rng1 = Ws1.Range("A1").CurrentRegion: Set Rng2 = Ws2.Range("A1").CurrentRegion
    Dim Rng1A As Range, Rng2B As Range
     Set Rng1A = Rng1.Range("A1:A" & Rng1.Rows.Count & ""): Set Rng2B = Rng2.Range("B1:B" & Rng2.Rows.Count & "")  ' : Set Rng2A = Rng2.Range("A1:A" & Rng2.Rows.Count & "")
    Rem 2 take each row in column A of 1.xlsx and compare it with  each row in column B of of 2.xlsx
    Dim Rws As Long
        For Rws = Rng1.Rows.Count To 2 Step -1
            If Rng1A.Item(Rws).Value = Rng2B.Item(Rws).Value Then
            ' Do nothing
            Else
             Rng2B.Item(Rws).EntireRow.Delete Shift:=xlUp
            End If
        Next Rws
    '    Wb1.Save
    '    Wb1.Close
    '    Wb2.Save
    '    Wb2.Close
    End Sub
    After run:-

    _____ Workbook: sample1.xlsx ( Using Excel 2007 32 bit )
    Row\Col
    A
    B
    C
    1
    2
    ACC
    150
    3
    DLF
    120
    4
    Worksheet: Sheet1

    _____ Workbook: sample2.xlsx ( Using Excel 2007 32 bit )
    Row\Col
    A
    B
    C
    D
    E
    F
    G
    H
    I
    1
    Exchange Symbol Series/Expiry Open High Low Prev Close LTP
    2
    NSE ACC EQ
    1034
    1068.75
    988
    998.45
    998.45
    3
    NSE DLF EQ
    235
    235.8
    203
    207.8
    207.8
    4
    5
    Worksheet: Sheet1


    I see no problem...
    This is almost a repeat of previous posts , with similar data files and results ... nothing much new here...
    Last edited by DocAElstein; 03-24-2020 at 02:47 PM.
    ….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
  •