+ Reply to Thread
Page 2 of 2 FirstFirst 12
Results 16 to 26 of 26

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

  1. #16
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    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.
    John

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

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

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

  2. #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

    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.

  3. #18
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    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.
    John

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

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

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

  4. #19
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    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?
    John

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

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

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

  5. #20
    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.

  6. #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

    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

  7. #22
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    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
    John

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

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

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

  8. #23
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    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
    John

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

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

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

  9. #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 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.

  10. #25
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    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.
    John

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

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

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

  11. #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

    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 07:54 AM. Reason: to add thanks to jaslake for his help

  12. #27
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

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

    You're welcome.
    John

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

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

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

+ 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.2.0