Page 3 of 9 FirstFirst 12345 ... LastLast
Results 21 to 30 of 83

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

  1. #21
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,458
    Rep Power
    10
    Same results again....

    ( I assume that STEP1U.xlsb is like previous 1.xlsx or sample1.xlsx
    and
    I assume 1.xls is like previous sample2.xlsx and 2.xlsx
    )

    Before:-

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

    _____ Workbook: 1.xls ( 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
    1014
    1030
    955.5
    998.45
    957.4
    3
    NSE ADANIPOWER EQ
    27.35
    27.75
    25.65
    25.65
    25.85
    4
    NSE DLF EQ
    390
    419.7
    350.05
    387.25
    352.4
    5
    NSE AMBUJACEM EQ
    145.1
    155.8
    142.55
    145.85
    145
    6
    NSE APOLLOHOSP EQ
    1250
    1265
    1124.15
    1158.65
    1127.05
    7
    Worksheet: 1-Sheet1


    Same macro as before ( http://www.excelfox.com/forum/showth...ll=1#post12887 )
    Run macro ...

    Code:
    Sub conditionally_delete() '  http://www.excelfox.com/forum/showthread.php/2436-conditionally-delete-entire-row?p=12890&viewfull=1#post12890
    Dim Wb1 As Workbook, Wb2 As Workbook, Ws1 As Worksheet, Ws2 As Worksheet
     Set Wb1 = Workbooks("STEP1U.xlsb") ' 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("1.xls") ' 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 macro

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

    _____ Workbook: 1.xls ( 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
    1014
    1030
    955.5
    998.45
    957.4
    3
    NSE DLF EQ
    390
    419.7
    350.05
    387.25
    352.4
    4
    NSE AMBUJACEM EQ
    145.1
    155.8
    142.55
    145.85
    145
    5
    NSE APOLLOHOSP EQ
    1250
    1265
    1124.15
    1158.65
    1127.05
    6
    Worksheet: 1-Sheet1


    Same results with similar data files.
    Third attempt at doing almost the same thing.
    No major difference in data or results.

    Results are correct, or at least they all do what you asked for....
    ACC column A in STEP1U.xlsb was equal to ACC column B in 1.xls then keep that entire row
    DLF column A in STEP1U.xlsb was not equal to ADANIPOWER column B in 1.xls , so row 3 in 1.xls , entire row, is deleted.
    .........if column A of 1.xlsx matches with column B of 2.xlsx then keep that entire row of 2.xlsx & if not matches then delete the entire row of 2.xlsx......
    Last edited by DocAElstein; 03-24-2020 at 06:36 PM.
    A Folk, A Forum, A Fuhrer ….

  2. #22
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0
    Now i understood what is the issue
    Doc Sir everything will be deleted in this sheet see the sample pic only the row that has ACC & dLf that row will be there and rest will be deleted
    see the sample pic and delete rest data except acc and dlf
    Attached Images Attached Images

  3. #23
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,458
    Rep Power
    10
    You must try to understand and explain what you want.

    These two are a contradiction:
    You say before:
    If column A of STEP1U.xls matches with column B of 1.xls then keep that entire row of 1.xls & if not matches then delete the entire row of 1.xls

    Now you say…. only the row that has ACC & dLf that row will be there

    . But

    in row 3 we have
    DLF column A __ ADANIPOWER column B

    In row 4 we have
    Column A empty __ DLF in column B

    So in those rows are no match = not matches

    DLF column A <> ADANIPOWER column B
    Column A empty <> DLF in column B

    Either
    You do not know yourself what you want
    Or
    You are totally unable to explain what you want.


    It is impossible to help you if you can not explain what it is you want.
    Either try again to explain, or give better data to show what you want


    As always, the problem is not the macro. The macros always do what you ask for. But you always ask wrong.
    God may know what you want. But I am not God.
    If you are unable to explain , then at least give much better data to show what you want.
    A Folk, A Forum, A Fuhrer ….

  4. #24
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,458
    Rep Power
    10
    How about this….

    What is wanted….
    ….. I have two workbooks, STEP1U.xlsb and 1.xls
    There is a data range in the first worksheet of both workbooks, from rows 2. ( I have a header range in rows 1 )

    Compare/ Consider column A in the first worksheet of STEP1U.xlsb with column B in the first worksheet of 1.xls. ( Ignore the first row – this is the header row )

    If the value in column B in the first worksheet of 1.xls is not in column A in the first worksheet of STEP1U.xlsb , then the entire row , ( in 1.xls ), containing that value is to be deleted.
    ( STEP1U.xlsb is not changed )

    Alternative short explanation: Delete all the rows in 1.xls whose value in column B is not to be found in column A in STEP1U.xlsb. ( STEP1U.xlsb remains unchanged )


    Before:-
    _____ Workbook: STEP1U.xlsb ( Using Excel 2007 32 bit )
    Row\Col
    A
    B
    C
    1
    2
    ACC
    100
    3
    DLF
    150
    4
    Worksheet: Sheet1

    _____ Workbook: 1.xls ( 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
    1014
    1030
    955.5
    998.45
    957.4
    3
    NSE ADANIPOWER EQ
    27.35
    27.75
    25.65
    25.65
    25.85
    4
    NSE DLF EQ
    390
    419.7
    350.05
    387.25
    352.4
    5
    NSE AMBUJACEM EQ
    145.1
    155.8
    142.55
    145.85
    145
    6
    NSE APOLLOHOSP EQ
    1250
    1265
    1124.15
    1158.65
    1127.05
    7
    Worksheet: 1-Sheet1


    After :-

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

    _____ Workbook: 1.xls ( 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
    1014
    1030
    955.5
    998.45
    957.4
    3
    NSE DLF EQ
    390
    419.7
    350.05
    387.25
    352.4
    4
    5
    Worksheet: 1-Sheet1





    Solution:-

    See here:
    http://www.excelfox.com/forum/showth...ll=1#post12896







    See also some Important notes on looping backwards when deleting rows
    http://www.excelfox.com/forum/showth...ll=1#post12902
    Last edited by DocAElstein; 03-27-2020 at 03:30 AM.
    A Folk, A Forum, A Fuhrer ….

  5. #25
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0
    Sorry Doc Sir for a mistake
    Thnx Alot Doc Sir for ur Great effort & Great help
    Problem Solved










    Moderator Translation:
    I have managed to totally confuse myself and everyome else by posting gibberish that I don’t even understand myself, so lets just chuck in one of my canned replies, ( that I don’t understand either ) , and just get out of here…
    The a few days ler after multiple cross duplicated poostings I am a total fucking idiot dim pig shit for brains, so just give me a macro that does what I want, even though most of the time I aint got a fucking clue myself what I want because I am such a total fucking spacko ... and never mind anyway, fuck off you mug, as some other twat gave me a macro at one of my many duplicated cross postings.......( Intro to Avinash Singh... https://excelfox.com/forum/showthrea...h-Introduction )
    Last edited by DocAElstein; 06-28-2020 at 07:20 PM.

  6. #26
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0

    replace the entire row

    there are two files file name sample1.xlsx & sample2.xlsx
    vba will be placed in a seperate file macro.xlsm
    sample1.xlsx is located at C:\Users\WolfieeeStyle\Desktop
    sample2.xlsx is located at C:\Users\WolfieeeStyle\Desktop\Upstox
    macro.xlsm is located at C:\Users\WolfieeeStyle\Desktop
    only macro.xlsm file is opened so we have to open rest 2 file by vba
    If column H of sample2.xlsx matches with Column D then look column B data of sample2.xlsx and find that data in sample1.xlsx in column B and after getting that data in sample1.xlsx in column B , copy that entire row of sample1.xlsx and paste that in sample2.xlsx in the same row
    all this is have to do by vba
    so plz have a look into this problem and help me in solving this problem sir
    sheet name can be anything
    Attached Images Attached Images

  7. #27
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,458
    Rep Power
    10

    macro will be placed in a separate file macro.xlsm
    only macro.xlsm file is opened so we have to open rest 2 file by vba
    sample1.xlsx is located at C:\Users\WolfieeeStyle\Desktop
    sample2.xlsx is located at C:\Users\WolfieeeStyle\Desktop\Upstox
    macro.xlsm is located at C:\Users\WolfieeeStyle\Desktop


    So … in macro.xlsm means Thisworkbook.Path is C:\Users\WolfieeeStyle\Desktop

    So … Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\sample1.xlsx") is Workbooks.Open(ThisWorkbook.Path & "\sample1.xlsx")
    and Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\Upstox\sample2.xlsx") is Workbooks.Open(ThisWorkbook.Path & "\Upstox\sample2.xlsx")

    sheet name can be anything
    So , for first worksheet, use item number, Worksheets.Item(1)
    ( second worksheet is Worksheets.Item(2)


    Before:
    _____ Workbook: sample1.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
    1014
    1030
    955.5
    998.45
    957.4
    3
    NSE ADANIPORTS EQ
    27.35
    27.75
    25.65
    25.65
    25.85
    4
    Worksheet: Tabelle1

    _____ 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
    1014
    1030
    955.5
    998.45
    957.4
    3
    NSE ADANIPORTS EQ
    27.35
    28
    29
    30
    27.35
    4
    Worksheet: Tabelle2


    Run macro:
    Code:
    ' ThisWorkbook is macro.xlsm is locatred at C:\Users\WolfieeeStyle\Desktop
    '  so  ThisWorkbook.path = "C:\Users\WolfieeeStyle\Desktop"
    Sub conditionally_replaceentirerow() '  http://www.excelfox.com/forum/showthread.php/2438-replace-the-entire-row
    Rem 1 Worksheets info
    Dim Wb1 As Workbook, Wb2 As Workbook, Ws1 As Worksheet, Ws2 As Worksheet
     Set Wb1 = Workbooks("sample1.xlsx")
     'Set Wb1 = Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\sample1.xlsx")
     'Set Wb1 = Workbooks.Open(ThisWorkbook.Path & "\sample1.xlsx")
     Set Ws1 = Wb1.Worksheets.Item(1) ' worksheet of first tab
     Set Wb2 = Workbooks("sample2.xlsx") '
     'Set Wb2 = Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\Upstox\sample2.xlsx")
     'Set Wb2 = Workbooks.Open(ThisWorkbook.Path & "\Upstox\sample2.xlsx")
     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
    
    Rem 2 Delete an entire row in Ws2 if value in column B is not anywhere in column A of Ws1
    Dim Rws As Long
        For Rws = 2 To Rng2.Rows.Count
            If Rng2.Range("H" & Rws & "").Value = Rng2.Range("D" & Rws & "").Value Then ' If column H of sample2.xlsx matches with Column D then
            Dim rngFnd As Range
             Set rngFnd = Rng1.Range("B2:B" & Rng2.Rows.Count).Find(what:=Rng2.Range("B" & Rws & "").Value, After:=Rng1.Range("B2"), LookIn:=xlValues, LookAt:=xlPart, searchorder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)  '   https://stackoverflow.com/questions/49094391/excel-vba-range-findnext-v-range-find-what-have-i-missed/49181464#49181464
                If rngFnd Is Nothing Then ' The value from column B in Ws2 was not found in column B of Ws1
                 ' do nothing
                Else
                 rngFnd.Offset(0, -1).Resize(1, Rng2.Columns.Count).Copy ' B offest -1 is A  resize to all columns in range B
                 Rng2.Range("A" & Rws & "").PasteSpecial Paste:=xlPasteAllUsingSourceTheme '
                End If
            Else
            ' column H of sample2.xlsx DOES NOT matches with Column D    do nothing
            End If
        Next Rws
        
    '    Wb1.Save
    '    Wb1.Close
    '    Wb2.Save
    '    Wb2.Close
    End Sub
    




    After Results

    _____ 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
    1014
    1030
    955.5
    998.45
    957.4
    3
    NSE ADANIPORTS EQ
    27.35
    27.75
    25.65
    25.65
    25.85
    4
    Worksheet: Tabelle2




    sample2.xlsx : https://app.box.com/s/xavyjz9q6ek3qknam42yif4f7l0qxdfa
    Attached Files Attached Files
    Last edited by DocAElstein; 03-27-2020 at 03:35 AM.
    A Folk, A Forum, A Fuhrer ….

  8. #28
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0
    Awesome Doc Sir Thnx Alot for ur Great Effort Doc Sir
    Problem Solved

  9. #29
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0

    Conditionally compare the data & delete entire row

    Hi, I am looking for a macro that will do the things as mentioned below

    If column J of 1.xls has buy & column H of 1.xls is not greater than column D of 1.xls then match column I data of 1.xls with column B of alert.xls and if it matches then delete that entire row of alert.xls
    If column J of 1.xls has a blank cell then match column I data of 1.xls with column B of alert.xls and if it matches then delete that entire row of alert.xls
    If column J of 1.xls has short & column H of 1.xls is Greater than than column D of 1.xls then match column I data of 1.xls with column B of alert.xls and if it matches then delete that entire row of alert.xls



    These are the three condition that macro will follow and based on that it will work
    all files are located in different path, macro will be placed in separte file macro.xlsm
    sheet name can be anything

    https://www.excelforum.com/excel-pro...ml#post5340172
    This link has solved the solution by considering alert.csv but now it is changed to alert.xls
    Attached Files Attached Files

  10. #30
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,458
    Rep Power
    10
    Quote Originally Posted by fixer View Post
    ...https://www.excelforum.com/excel-pro...ml#post5340172
    This link has solved the solution by considering alert.csv but now it is changed to alert.xls
    The solution here, https://www.excelforum.com/excel-pro...ml#post5340172 , was optimised for using a text file as Alert.csv , using advanced Text file data manipulation techniques
    There is no useful coding information there which can be applied to the current requirement : The macro from jindon is no use in the current question here.




    If column J of 1.xls has BUY & column H of 1.xls is not greater than column D of 1.xls then match column I data of 1.xls with column B of alert.xls and if it matches then delete that entire row of alert.xls
    If column J of 1.xls has a blank cell then match column I data of 1.xls with column B of alert.xls and if it matches then delete that entire row of alert.xls
    If column J of 1.xls has SHORT & column H of 1.xls is Greater than column D of 1.xls then match column I data of 1.xls with column B of alert.xls and if it matches then delete that entire row of alert.xls
    row 3 & row 4 & row 5 will be deleted after running the macro


    Before:
    _____ Workbook: 1.xls ( Using Excel 2007 32 bit )
    Row\Col A B C D E F G H I J
    1 Exchange Symbol Series/Expiry Open High Low Prev Close LTP
    2 NSE ACC EQ
    1183.72
    1240
    1161.6
    1227.1
    1227.1
    22
    BUY
    3 NSE ADANIENT EQ
    139.38
    141.2
    136.6
    138.1
    140
    25
    BUY
    4 NSE ADANIPORTS EQ
    311.85
    315
    306.55
    310.6
    312
    15083
    SHORT
    5 NSE ADANIPOWER EQ
    33.165
    34.5
    32.85
    33
    33.2
    17388
    SHORT
    6 NSE AMARAJABAT EQ
    594
    613.5
    586.9
    592.55
    594
    100
    7 NSE ASIANPAINT EQ
    1584.488
    1625
    1555.4
    1617.9
    1617.9
    236
    BUY
    Worksheet: 1-Sheet1 9 June

    _____ Workbook: Alert.xls ( Using Excel 2007 32 bit )
    Row\Col A B C D E F G H I J K
    1 NSE
    236
    6
    >
    431555
    A GTT
    2 NSE
    25
    6
    >
    431555
    A GTT
    3 NSE
    15083
    6
    >
    431555
    A GTT
    4 NSE
    17388
    6
    >
    431555
    A GTT
    5 NSE
    100
    6
    >
    431555
    A GTT
    6 NSE
    22
    6
    >
    431555
    A GTT
    Worksheet: Alert 9 June xls

    After running macro here: https://excelfox.com/forum/showthrea...ll=1#post13733


    _____ Workbook: Alert.xls ( Using Excel 2007 32 bit )
    Row\Col A B C D E F G H I J K
    1 NSE
    236
    6
    >
    431555
    A GTT
    2 NSE
    25
    6
    >
    431555
    A GTT
    3 NSE
    22
    6
    >
    431555
    A GTT
    4
    5
    6
    Worksheet: Alert 9 June xls



    Alan





    macro.xlsm : https://app.box.com/s/z358r7tbc9hzthi539dlj49jsf4gyg8p
    1.xls : https://app.box.com/s/38aoip5xi7018y9syt0xe4g04u95l6xk
    Alert.xls : https://app.box.com/s/ectstkrcfnuozys9tmdd0qi3tdvyxb3w
    A Folk, A Forum, A Fuhrer ….

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
  •