Consulting

Results 1 to 20 of 20

Thread: Compare lines in two sheets

  1. #1
    VBAX Tutor
    Joined
    Sep 2012
    Location
    London
    Posts
    237
    Location

    Compare lines in two sheets

    Hello,
    Is there a way in VBA Excel one full line to compare with each other? In sheet1 A1:H1 to A2500:H2500 has information, on sheet2 A1:H1 to A10000:H10000 has other information. I need to do the following thing, line A1:H1 on Sheet1 to compare it to each row of sheet2, if in sheet2 row is found, whose cells contain the same information as in A1:H1 on Sheet1 to earn value I1, if there is no row with the same information, I1 is empty, the same should be repeated with the other 2499 rows.
    Thanks in advance for the help rendered by you.
    Attached Files Attached Files

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    Sub blah()
    With Sheets("Sheet1")
      For Each rw1 In Intersect(.UsedRange.EntireRow, .Range("A:H")).Rows
        For Each rw2 In Intersect(Sheets("Sheet2").UsedRange.EntireRow, Sheets("Sheet2").Range("A:H")).Rows
          For i = 1 To 8
            If Not rw1.Cells(i) = rw2.Cells(i) Then Exit For
          Next i
          If i = 9 Then .Cells(rw1.Row, "I").Value = "Line " & rw2.Row
        Next rw2
      Next rw1
    End With
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    or

    Sub M_snb()
        sn = Filter([transpose(if(sheet2!A1:A100="","~",sheet2!A1:A100&sheet2!B1:B100&sheet2!C1:C100&sheet2!D1:D100&sheet2!E1:E100&sheet2!F1:F100&sheet2!G1:G100&sheet2!H1:H100))], "~", False)
        
        sp = Sheet1.Cells(1).CurrentRegion
        For j = 1 To UBound(sp)
           If UBound(Filter(sn, Join(Application.Index(sp, j, 0), ""))) > -1 Then MsgBox "I found  " & Join(Application.Index(sp, j, 0), "")
        Next
    End Sub

  4. #4
    VBAX Tutor
    Joined
    Sep 2012
    Location
    London
    Posts
    237
    Location
    Many thanks p45cal, it's possible.
    In your example snb, just let me climb the inscription, but in column "I:I", I do not show it which row is the record of "Sheet2".
    Thank you very much.

    This is further off-topic question: Why can not you give reputations?

  5. #5
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    Just had a thought, are there multiple rows to find on sheet2? If not, I should have aborted the search each time a row was found - at the moment it shows only the last row found, it would be faster. If there are multiple lines, do you want a list of all of them?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    You could have adapted the code yourself:

    Sub M_snb() 
        on error resume next
        sn = Filter([transpose(If(sheet2!A1:A100="","~",sheet2!A1:A100&sheet2!B1:B100&sheet2!C1:C100&sheet2!D1:D100&sheet2!E1:E100&sheet2!F1:F100&sheet2!G1:G100&sheet2!H1:H100))], "~", False) 
         
        sp = Sheet1.Cells(1).CurrentRegion 
        For j = 1 To UBound(sp) 
            sheet1.cells(j, ubound(sp,2)+2)=Application.match(Join(Application.Index(sp, j, 0), ""),sn,0) 
        Next 
    End Sub

  7. #7
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    Quote Originally Posted by k0st4din View Post

    This is further off-topic question: Why can not you give reputations?
    just click the asterisk * below user name in any post.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  8. #8
    VBAX Tutor
    Joined
    Sep 2012
    Location
    London
    Posts
    237
    Location
    Hello p45cal, with the first macro that make it work the way I want (I'll attach a picture to see), the second macro that made snb and he now works.
    I do not understand your question - >> "If there are multiple lines, do you want a list of all of them?" - What do you mean? If you do it the macro to try what would be the difference and I'll try it.
    mancubus here is made ​​by me picture by pressing the star that does not allow me -> Link to download
    Attached Images Attached Images

  9. #9
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    Quote Originally Posted by k0st4din View Post
    Hello p45cal"If there are multiple lines, do you want a list of all of them?" - What do you mean?
    For each line on sheet1, you want to know where it is to be found on sheet2. What if, for a given line on sheet1, there are more than one similar lines on sheet2?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  10. #10
    VBAX Tutor
    Joined
    Sep 2012
    Location
    London
    Posts
    237
    Location
    You have no idea how right you are. I never thought of that.
    I'd appreciate if you did for me. Thus if sheet2 has more rows with the same information, I'll bear it in sheet1. That's what you have in mind?
    Thank you very much.

  11. #11
    This is very educational content and written well for a change. It's nice to see that some people still understand how to write a quality post.

  12. #12
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    try:
    Sub blah2()
    With Sheets("Sheet1")
      For Each rw1 In Intersect(.UsedRange.EntireRow, .Range("A:H")).Rows
        With .Cells(rw1.Row, "I")
          .ClearContents
          For Each rw2 In Intersect(Sheets("Sheet2").UsedRange.EntireRow, Sheets("Sheet2").Range("A:H")).Rows
            For i = 1 To 8
              If Not rw1.Cells(i) = rw2.Cells(i) Then Exit For
            Next i
            If i = 9 Then
              If Len(.Value) = 0 Then
                .Value = "Line " & rw2.Row
              Else
                .Value = Replace(.Value, "Line ", "Lines ") & ", " & rw2.Row
              End If
            End If
          Next rw2
        End With
      Next rw1
    End With
    End Sub
    If it's slow I can speed it up.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  13. #13
    VBAX Tutor
    Joined
    Sep 2012
    Location
    London
    Posts
    237
    Location
    This is enough, I just have no words with which to say thank you.
    I will say this I thank you for being there to help us unknowing how to do it.
    Bow before you.

  14. #14
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    Were there any duplicate lines on sheet2?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  15. #15
    VBAX Tutor
    Joined
    Sep 2012
    Location
    London
    Posts
    237
    Location
    Yes, it was in the original file after I placed the macro. There were two identical lines.

  16. #16
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    so this could work too:

    Sub M_snb() 
        On Error Resume Next
     
        sn = Filter([transpose(If(sheet2!A1:A100="","~",sheet2!A1:A100&sheet2!B1:B100&sheet2!C1:C100&sheet2!D1:D100&sheet2!E1:E100&sheet2!F1:F100&sheet2!G1:G100&sheet2!H1:H100))], "~", False) 
         
        sp = Sheet1.Cells(1).CurrentRegion 
        For j = 1 To UBound(sp) 
            sheet1.cells(j, UBound(sp,2)+2)=Application.match(Join(Application.Index(sp, j, 0), ""),sn,0)
            sn(Application.match(Join(Application.Index(sp, j, 0), ""),sn,0)-1)=""
        Next 
    End Sub
    Last edited by snb; 10-25-2013 at 06:46 AM.

  17. #17
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    Quote Originally Posted by snb View Post
    so this could work too:
    It doesn't seem to.
    Don't forget that sn is zero based so:
    sn(Application.match(Join(Application.Index(sp, j, 0), ""),sn,0))=""
    deletes the wrong entry and I think should be:
    sn(Application.match(Join(Application.Index(sp, j, 0), ""),sn,0)-1)=""

    but having deleted the entry it doesn't then look for another.
    Finally, hopefully there should be no blanks in column A of sheet2 amongst the data.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  18. #18
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    @p45cal


    You are right about:
    sn(Application.match(Join(Application.Index(sp, j, 0), ""),sn,0)-1)=""

    Thank you, I amended the code.

    I don't think you are right here:
    but having deleted the entry it doesn't then look for another.
    every time a match is found the match will be deleted, so the next time the next match will be found

    Nor here:
    Finally, hopefully there should be no blanks in column A of sheet2 amongst the data.
    They have been singled out by the first line in the code.

  19. #19
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    Well, I tried this on sample data. Regarding "it doesn't then look for another", you say
    Quote Originally Posted by snb View Post
    so the next time the next match will be found
    but there won't be a next time; the snippet:
    For j = 1 To UBound(sp) 
            sheet1.cells(j, UBound(sp,2)+2)=Application.match(Join(Application.Index(sp, j, 0), ""),sn,0) 
            sn(Application.match(Join(Application.Index(sp, j, 0), ""),sn,0)-1)="" 
        Next
    executes the code inside the loop once, and once only, per value of j (the rows on sheet1), so Match on the first line is only done once (per value of j), it also needs to be done again until no more matches are found, so a While..Wend or Do Loop Until might be needed within the For j= loop.

    Regarding "no blanks in column A of sheet2 amongst the data", yes, it does filter those out, even if they are amongst the data to be searched, so if there are some, the indices of sn will no longer tally with the row numbers in sheet2 below any blanks in column A.

  20. #20
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    Amended into:

    Sub M_snb()
        sn = Filter([transpose(If(sheet2!A1:A100="","~",sheet2!A1:A100&sheet2!B1:B100&sheet2!C1:C100&sheet2!D1:D100&sheet2!E1:E100&sheet2!F1:F100&sheet2!G1:G100&sheet2!H1:H100))], "")
        sp = Sheet1.Cells(1).CurrentRegion
    
        For j = 1 To UBound(sp)
            c00 = Join(Application.Index(sp, j, 0), "")
    
            Do Until UBound(Filter(sn, c00)) = -1
                Sheet1.Cells(j, UBound(sp, 2) + 2) = Sheet1.Cells(j, UBound(sp, 2) + 2) & ";" & Application.Match(c00, sn, 0)
                sn(Application.Match(c00, sn, 0) - 1) = ""
            Loop
        Next
    End Sub

Posting Permissions

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