+ Reply to Thread
Results 1 to 27 of 27

Comapring two excel worksheets and adding or deleting rows as needed

Hybrid View

  1. #1
    Registered User
    Join Date
    12-27-2011
    Location
    Bham, AL
    MS-Off Ver
    Excel 2007
    Posts
    14

    Smile Comapring two excel worksheets and adding or deleting rows as needed

    Good Afternoon,

    I am new to VBA coding and need help with excel VBA.

    I have one work book with two worksheets, one is a current worksheet (S1) the other is an update version of the first(S2). All columns are the same and each will contain approximately 20,000 rows of data in.

    1. I want to compare each cell in column A of S2 to all of column A in S1
    a. If cell from S2 is equal to any cell in column A of S1 then do nothing
    b. If cell from S2 is not equal to any cell in column A of S1 then add the corresponding row data to S1

    2. Compare each cell in cloumn A of S1 to each cell in column A of S2
    a. If cell from S1 is equal to any cell in column A of S2 then do nothing
    b. If cell from column A of S1 is not equal to any cell in column A of S2 then do:
    1. Create new worksheet (S3) and add corresponding row of data to new row in S3
    2. Delete corresponding row of data from S1

    If you need a sample of the workbook I can sanitize and supply, but preferable on to give out.
    Last edited by dmreno; 12-28-2011 at 09:34 AM. Reason: code provided worked perfectly!

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Comapring two excel worksheets and adding or deleting rows as needed

    Hi dmreno

    Welcome to the Forum!

    Try this code. The code assumes you have a header row in Row 1 of each worksheet. Let me know of issues
    Option Explicit
    Sub test()
        Dim lr1 As Long
        Dim lr2 As Long
        Dim lr3 As Long
        Dim nr1 As Long
        Dim nr3 As Long
        Dim ws1 As Worksheet
        Dim ws2 As Worksheet
        Dim ws3 As Worksheet
        Dim rng1 As Range
        Dim rng2 As Range
        Dim cel1 As Range
        Dim cel2 As Range
        Dim FindString As String
    
        Set ws1 = ActiveWorkbook.Sheets("Sheet1")
        Set ws2 = ActiveWorkbook.Sheets("Sheet2")
        Set ws3 = ActiveWorkbook.Sheets("Sheet3")
    
        lr1 = ws1.Range("A" & Rows.Count).End(xlUp).Row
        lr2 = ws2.Range("A" & Rows.Count).End(xlUp).Row
        nr1 = lr1 + 1
        Set rng2 = ws2.Range("A2:A" & lr2)
    
        For Each cel2 In rng2
            FindString = cel2.Value
            If Trim(FindString) <> "" Then
                With ws1.Range("A2:A" & lr1)
                    Set rng1 = .Find(What:=FindString, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
                    If rng1 Is Nothing Then
                        cel2.EntireRow.Copy
                        ws1.Range("A" & nr1).PasteSpecial
                        nr1 = nr1 + 1
                        Application.CutCopyMode = False
                    End If
                End With
            End If
        Next cel2
    
        lr1 = ws1.Range("A" & Rows.Count).End(xlUp).Row
        lr3 = ws3.Range("A" & Rows.Count).End(xlUp).Row
        nr3 = lr3 + 1
        Set rng1 = ws1.Range("A2:A" & lr1)
    
        For Each cel1 In rng1
            FindString = cel1.Value
            If Trim(FindString) <> "" Then
                With ws2.Range("A2:A" & lr2)
                    Set rng2 = .Find(What:=FindString, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
                    If rng2 Is Nothing Then
                        cel1.EntireRow.Copy
                        ws3.Range("A" & nr3).PasteSpecial
                        nr3 = nr3 + 1
                        cel1.EntireRow.ClearContents
                        Application.CutCopyMode = False
                    End If
                End With
            End If
        Next cel1
        With ws1
            lr1 = .Range("A" & Rows.Count).End(xlUp).Row
            .Range("A1:A" & lr1).AutoFilter Field:=1, Criteria1:="="
            .UsedRange.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
            .AutoFilterMode = False
        End With
    End Sub
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Registered User
    Join Date
    12-27-2011
    Location
    Bham, AL
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Comapring two excel worksheets and adding or deleting rows as needed

    jaslake thanks for the help. This code worked perfectly on a sample of about 30 rows in each spreadsheet. the real ones have 20K so will take a bit to run, but I have no doubt that it will work as well. You are a god send. I am hoping to learn this myself, I have used visual basic and basic in past but has been over 15 years since using it. so rust is thick. Let me know what else I need to do to ensure you get credit and I close this out properly.

    Thanks again for the help.

    David

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Comapring two excel worksheets and adding or deleting rows as needed

    Hi dmreno

    Actually the code is significantly less complex with far fewer IO's. I'm assuming you've run the procedure on a COPY of the real file???
    Assuming you did, kill the procedure (hold down the escape key...it may take a bit...make a note of the line of code where the procedure break point is) and let me know that line.
    Assuming you didn't, I have to assume you have a BACKUP???

    Let me play with it for a bit...I can look at eliminating the Copy/Paste stuff...that may speed it up.

  5. #5
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Comapring two excel worksheets and adding or deleting rows as needed

    Hi David
    You're welcome...glad I could help.

  6. #6
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Comapring two excel worksheets and adding or deleting rows as needed

    Hi David

    I should of asked and didn't...which file ALWAYS has the fewer records...OldMel or NewMel?

  7. #7
    Registered User
    Join Date
    12-27-2011
    Location
    Bham, AL
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Comapring two excel worksheets and adding or deleting rows as needed

    jaslake,

    did you recieve the message sent regarding additional help with this code?

  8. #8
    Registered User
    Join Date
    12-27-2011
    Location
    Bham, AL
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Comapring two excel worksheets and adding or deleting rows as needed

    When comparing current sheet col A cells to corresponding updated sheet col A cells if they were the same, we did nothing ( in the below code), but I actually need to have the code do: If col A cells are equal then I need to copy the information from the corresponding row # for columns H, I & J (updated sheet) and place it into the corresponding row # in columns H, I & J in the current sheet.

    Those three columns of information need to be updated whether or not the col A cell is new or not.

    I hope this makes sense to you. I dont think it is very difficult to do.

    I understand your programing logic, but I do not know the code well enough to attempt to modify myself.

    Thank you in advance for this last bit of assistance.

  9. #9
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Comapring two excel worksheets and adding or deleting rows as needed

    Hi dmreno

    Yes I received your PM. Been on the road for the last several days with Family responsibilities...haven't paid too much attention to the Forum. I'll look at this probably tomorrow.

  10. #10
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Comapring two excel worksheets and adding or deleting rows as needed

    Hi David
    See if this code works any better
    Option Explicit
    Sub test()
        Dim lr1 As Long
        Dim lr2 As Long
        Dim ws1 As Worksheet
        Dim ws2 As Worksheet
        Dim rng1 As Range
        Dim rng2 As Range
        Dim cel2 As Range
        Dim FindString As String
    
        Application.ScreenUpdating = False
        Set ws1 = Sheet1    'NewMel
        Set ws2 = Sheet2    'OldMel
    
        lr1 = ws1.Range("A" & Rows.Count).End(xlUp).Row
        lr2 = ws2.Range("A" & Rows.Count).End(xlUp).Row
        Set rng2 = ws2.Range("A2:A" & lr2)
    
        'With each value in OldMel Column A, find it in
        'NewMel Column A;  if found, replace NewMel row with OldMel row;
        'if not found do nothing
    
        For Each cel2 In rng2    'OldMel
            FindString = cel2.Value
            If Trim(FindString) <> "" Then
                With ws1.Range("A2:A" & lr1)
                    Set rng1 = .Find(What:=FindString, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
                    If Not rng1 Is Nothing Then
    '                                        Application.Goto rng1, True
    '                    ws2.Range("A" & cel2.Row).EntireRow.Copy
    '                    ws1.Range("A" & rng1.Row).PasteSpecial
                        ws1.Range("A" & rng1.Row).Resize(1, 21).Value = ws2.Range("A" & cel2.Row).Resize(1, 21).Value
    '                    Application.CutCopyMode = False
                    End If
                End With
            End If
        Next cel2
        Application.ScreenUpdating = True
    End Sub

  11. #11
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Comapring two excel worksheets and adding or deleting rows as needed

    Hi dmreno
    Dummy up a file that represents what you're working with. Please show examples of S1 and S2. Include S3 that demonstrates what you wish to see in S3. If you choose not to do so, I'll need to do so and I'd prefer not to guess.

  12. #12
    Registered User
    Join Date
    12-27-2011
    Location
    Bham, AL
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Comapring two excel worksheets and adding or deleting rows as needed

    I will get you something by end of business today.

  13. #13
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Comapring two excel worksheets and adding or deleting rows as needed

    Hi David

    Then try this code...let me know what works...what doesn't
    Option Explicit
    Sub test()
        Dim lr1 As Long
        Dim lr2 As Long
        Dim ws1 As Worksheet
        Dim ws2 As Worksheet
        Dim rng1 As Range
        Dim rng2 As Range
        Dim cel2 As Range
        Dim FindString As String
        Dim xlCalc As XlCalculation
        
        On Error GoTo ExitPoint
        With Application
            xlCalc = .Calculation
            .Calculation = xlCalculationManual
            .EnableEvents = False
            .ScreenUpdating = False
        End With
        
        Set ws1 = Sheet1    'NewMel
        Set ws2 = Sheet2    'OldMel
    
        lr1 = ws1.Range("A" & Rows.Count).End(xlUp).Row
        lr2 = ws2.Range("A" & Rows.Count).End(xlUp).Row
        Set rng2 = ws2.Range("A2:A" & lr2)
    
        'With each value in OldMel Column A, find it in
        'NewMel Column A;  if found, replace NewMel row with OldMel row;
        'if not found do nothing
    
        For Each cel2 In rng2    'OldMel
            FindString = cel2.Value
            If Trim(FindString) <> "" Then
                With ws1.Range("A2:A" & lr1)
                    Set rng1 = .Find(What:=FindString, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
                    If Not rng1 Is Nothing Then
                        
                        ws1.Range("A" & rng1.Row).Resize(1, 21).Value = ws2.Range("A" & cel2.Row).Resize(1, 21).Value
                    End If
                End With
            End If
        Next cel2
    ExitPoint:
        With Application
            .Calculation = xlCalc
            .EnableEvents = True
            .ScreenUpdating = True
        End With
    End Sub

  14. #14
    Registered User
    Join Date
    12-27-2011
    Location
    Bham, AL
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Comapring two excel worksheets and adding or deleting rows as needed

    here is the workbook you asked for
    Attached Files Attached Files

  15. #15
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Comapring two excel worksheets and adding or deleting rows as needed

    Hi David...the two are the same code and do the same things the same way. The second version turns off calculation so I would expect it to be at least marginally faster.

  16. #16
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Comapring two excel worksheets and adding or deleting rows as needed

    Hi dmreno
    I've gotta admit, I'm a bit confused about what you're after. The code in the attached does this:

    First
    With each value in Current Sheet Column A, find it in
    Update Sheet Column A; if found, copy Update Sheet Columns
    H, I and J values to Current Sheet Columns H, I and J

    Second
    With each value in Update Sheet Column A, find it in
    Current Sheet Column A; if not found, copy Update Sheet entire row
    to the next row on Current Sheet

    The results I get look nothing like the results you get. Explain a bit further and perhaps we can get on the same page.
    Attached Files Attached Files

  17. #17
    Registered User
    Join Date
    12-27-2011
    Location
    Bham, AL
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Comapring two excel worksheets and adding or deleting rows as needed

    Jaslake

    I have been looking at this all wrong. Can you contact me to discuss? I can supply phone number in private message. Then I will post to the thread what exactly I need and we can wrap this up.

  18. #18
    Registered User
    Join Date
    12-27-2011
    Location
    Bham, AL
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Comapring two excel worksheets and adding or deleting rows as needed

    jaslake

    I have attached a new file to use.

    For instance:

    For each T# in the NewMEL sheet I need to find it in the OldMEL. If it is found in the OldMEL sheet, then copy OldMEL data for that T# row into NewMEL row.

    If T# from NewMEL cannot be found in OldMEL then do nothing.
    Attached Files Attached Files

  19. #19
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Comapring two excel worksheets and adding or deleting rows as needed

    You're welcome.

  20. #20
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Comapring two excel worksheets and adding or deleting rows as needed

    Hi dmreno

    This code is in the attached and does this:
    With each value in OldMel Column A, find it in NewMel Column A; if found, replace NewMel row with OldMel row; if not found do nothing.

    Let me know of issues.
    Option Explicit
    Sub test()
        Dim lr1 As Long
        Dim lr2 As Long
        Dim ws1 As Worksheet
        Dim ws2 As Worksheet
        Dim rng1 As Range
        Dim rng2 As Range
        Dim cel2 As Range
        Dim FindString As String
    
        Application.ScreenUpdating = False
        Set ws1 = Sheet1    'NewMel
        Set ws2 = Sheet2    'OldMel
    
        lr1 = ws1.Range("A" & Rows.Count).End(xlUp).Row
        lr2 = ws2.Range("A" & Rows.Count).End(xlUp).Row
        Set rng2 = ws2.Range("A2:A" & lr2)
    
        'With each value in OldMel Column A, find it in
        'NewMel Column A;  if found, replace NewMel row with OldMel row;
        'if not found do nothing
    
        For Each cel2 In rng2    'OldMel
            FindString = cel2.Value
            If Trim(FindString) <> "" Then
                With ws1.Range("A2:A" & lr1)
                    Set rng1 = .Find(What:=FindString, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
                    If Not rng1 Is Nothing Then
                        '                    Application.Goto rng1, True
                        ws2.Range("A" & cel2.Row).EntireRow.Copy
                        ws1.Range("A" & rng1.Row).PasteSpecial
                        Application.CutCopyMode = False
                    End If
                End With
            End If
        Next cel2
        Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files

  21. #21
    Registered User
    Join Date
    12-27-2011
    Location
    Bham, AL
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Comapring two excel worksheets and adding or deleting rows as needed

    I am running it on the real file, how long do you think it will take to run through 22,00o rows?

  22. #22
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Comapring two excel worksheets and adding or deleting rows as needed

    Hi dmreno

    A while I'd think...let me know how it goes.

  23. #23
    Registered User
    Join Date
    12-27-2011
    Location
    Bham, AL
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Comapring two excel worksheets and adding or deleting rows as needed

    It is at 3 hours now and still running. is it vastly different from what the first code was doing? That code took less than 30 minutes to run through the 22,000 rows. Tha is why I am asking the question.

  24. #24
    Registered User
    Join Date
    12-27-2011
    Location
    Bham, AL
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Comapring two excel worksheets and adding or deleting rows as needed

    It locked up but did not see your post till now. I am going to run it again, right now.

  25. #25
    Registered User
    Join Date
    12-27-2011
    Location
    Bham, AL
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Comapring two excel worksheets and adding or deleting rows as needed

    here is the line of code that was highlighted. the current situation has the newmel file with more rows than the oldmel but not sure if that will always be the case.


    Application.CutCopyMode = False

  26. #26
    Registered User
    Join Date
    12-27-2011
    Location
    Bham, AL
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Comapring two excel worksheets and adding or deleting rows as needed

    It appears that both codes work and provide same results. What is the difference in the two? Is one faster running than the other? I will test on a backup version of the 22k row file and let you know.

  27. #27
    Registered User
    Join Date
    12-27-2011
    Location
    Bham, AL
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Comapring two excel worksheets and adding or deleting rows as needed

    Hey John,

    I finally was able to get it to run on the the main file. Both worked and it took only 5 minutes to run on the 22 K row of information. We are still not sure why they were hangin up on the older files I had, but I did creat a whole new one and it ran perfectly. Thanks again for all your help..

    Once again you came through with excellent code that works flawlessly and I greatly appreciate it very much.

    David
    Last edited by dmreno; 01-31-2012 at 08:54 AM. Reason: to add thanks to jaslake for his help

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1