+ Reply to Thread
Results 1 to 24 of 24

Macro to compare columns and copy rows.

Hybrid View

  1. #1
    Registered User
    Join Date
    09-12-2012
    Location
    Richmond, VA
    MS-Off Ver
    Varies but mostly 365
    Posts
    15

    Macro to compare columns and copy rows.

    My last post just timed out so I lost all my notes... Trying again...

    I'm trying to create a macro to scrub new data and update my records for a rolling 30 day dashboard and other charts that feed off the main dataset...

    Example file attached. I need to scan the 'New' worksheet tab, Column A [Record] against the 'Old' worksheet tab, also Column A [Record]. If a match is found then copy the row of "New" data over the row of "Old" data. If a match is not found append the "New" row to the end of the "Old" sheet / table.

    I also need code to then delete rows from the 'Old' sheet that are 30 days or older than today.

    Please let me know if there is any additional information I can provide, sorry my last post timed out and I lost all the extras I had included.

    Thank you,
    Jason.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    11-29-2022
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    435

    Re: Macro to compare columns and copy rows.

    Option Explicit
    
    Sub DoThings()
    
        Dim wsN As Worksheet, wsO As Worksheet
        Dim lrow As Integer, nrow As Integer, i As Integer
        Dim str As String, str2 As String
        Dim fndrng As Range
        
        Set wsN = ThisWorkbook.Sheets("NEW")
        Set wsO = ThisWorkbook.Sheets("OLD")
        
        lrow = wsN.Range("A:A").Find(what:="*", searchdirection:=xlPrevious).Row
        nrow = wsO.Range("A:A").Find(what:="*", searchdirection:=xlPrevious).Row + 1
        
        i = 2
        
        Do While i <= lrow
            Set fndrng = wsO.Range("A:A").Find(what:=wsN.Cells(i, 1))
            str = "A" & i & ":F" & i
            
            If fndrng Is Nothing Then
                str2 = "A" & nrow
                nrow = nrow + 1
                
            Else
                str2 = "A" & fndrng.Row
                
            End If
                
            wsN.Range(str).Copy
            wsO.Range(str2).PasteSpecial xlPasteValuesAndNumberFormats
            Application.CutCopyMode = False
            
            'Enable the two rows below if you want to delete the row from New after it has been
            'processed. Remove the ' to un-comment. Also, if you enable them, delete the row below
            'them.
            
    '        wsN.Rows(i).Delete
    '        lrow = lrow - 1
    
            i = i + 1   'Delete this if you enable the two commands above
            
        Loop
    
    End Sub

  3. #3
    Registered User
    Join Date
    09-12-2012
    Location
    Richmond, VA
    MS-Off Ver
    Varies but mostly 365
    Posts
    15

    Re: Macro to compare columns and copy rows.

    Thank you so much, I've matched the specifics up to my main files and running it now. I'm working with only about 3000-3500 rows in the 'New' data tab and it's running on close to 10 minutes now. I added application.screenupdating = false/true to the beginning and end of the sub but it's still running long. Do you think there is anything I can change to make it run faster?

    I've not tried the 30 days portion yet.

    Thanks again !!!

    EDIT: I had to stop the macro after about 15 - 20 minutes, and it appears to have done only 1/2 the data.

    EDIT2: I'm going to try using range.paste instead of .PasteSpecial xlPasteValuesAndNumberFormats on the remaining data and see if there is a duration difference.
    Last edited by JasonMS; 12-16-2022 at 09:57 AM. Reason: Update

  4. #4
    Valued Forum Contributor
    Join Date
    11-29-2022
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    435

    Re: Macro to compare columns and copy rows.

    And here is the code to remove stuff over 30 days. You can add the command "Call RemoveOldStuff" to the end of the previous script to flow into this one.

    Sub RemoveOldStuff()
    
        Dim ws As Worksheet
        Dim ddate As Date
        Dim i As Integer, lrow As Integer
        
        Set ws = ThisWorkbook.Sheets("OLD")
        ddate = Date - 30
        lrow = ws.Range("A:A").Find(what:="*", searchdirection:=xlPrevious).Row
        i = 2
        
        Do While i <= lrow
            If ws.Cells(i, 4) < ddate Then
                ws.Rows(i).Delete
                i = i - 1
                lrow = lrow - 1
                
            End If
            
            i = i + 1
            
        Loop
    
    End Sub

  5. #5
    Valued Forum Contributor
    Join Date
    11-29-2022
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    435

    Re: Macro to compare columns and copy rows.

    Can also try adding the below along with screenupdating.
    Application.Calculation = xlCalculationManual     'then xlCalculationAutomatic   at the end
    This is an alternate method of moving the data over:

    Sub DoThings()
    
        Dim wsN As Worksheet, wsO As Worksheet
        Dim lrow As Integer, nrow As Integer, i As Integer, j As Integer, rw As Integer
        Dim fndrng As Range
        
        Set wsN = ThisWorkbook.Sheets("NEW")
        Set wsO = ThisWorkbook.Sheets("OLD")
        
        lrow = wsN.Range("A:A").Find(what:="*", searchdirection:=xlPrevious).Row
        nrow = wsO.Range("A:A").Find(what:="*", searchdirection:=xlPrevious).Row + 1
        
        i = 2
        
        Do While i <= lrow
            Set fndrng = wsO.Range("A:A").Find(what:=wsN.Cells(i, 1))
            
            If fndrng Is Nothing Then
                rw = nrow
                nrow = nrow + 1
                
            Else
                rw = fndrng.Row
                
            End If
            
            For j = 1 To 6
                wsO.Cells(rw, j) = wsN.Cells(i, j)
                
            Next j
            
            'Enable the two rows below if you want to delete the row from New after it has been
            'processed. Remove the ' to un-comment. Also, if you enable them, delete the row below
            'them.
            
    '        wsN.Rows(i).Delete
    '        lrow = lrow - 1
    
            i = i + 1   'Delete this if you enable the two commands above
            
        Loop
    
    End Sub

  6. #6
    Registered User
    Join Date
    09-12-2012
    Location
    Richmond, VA
    MS-Off Ver
    Varies but mostly 365
    Posts
    15

    Re: Macro to compare columns and copy rows.

    Cool, I'll try that method next!
    Last edited by JasonMS; 12-16-2022 at 10:16 AM.

  7. #7
    Registered User
    Join Date
    09-12-2012
    Location
    Richmond, VA
    MS-Off Ver
    Varies but mostly 365
    Posts
    15

    Re: Macro to compare columns and copy rows.

    Ok, your second method worked in a snap, less than 5 minutes for the remainder of data and it was complete. I'll run a few more tests before I can use it in our day-to-day but I can't thank you enough.

    If you have a moment can you explain the major difference between your first method to the second as to why it would be so much faster?

  8. #8
    Valued Forum Contributor
    Join Date
    11-29-2022
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    435

    Re: Macro to compare columns and copy rows.

    The first method used copy and paste. The second method... hmm... hard to explain. It just sets one cell equal to the other so I guess maybe it is using internal memory? Not quite sure how to explain that better.

  9. #9
    Registered User
    Join Date
    09-12-2012
    Location
    Richmond, VA
    MS-Off Ver
    Varies but mostly 365
    Posts
    15

    Re: Macro to compare columns and copy rows.

    It's all good, it works and works well. So thankful for your help, have a great day and end of year!

  10. #10
    Valued Forum Contributor
    Join Date
    11-29-2022
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    435

    Re: Macro to compare columns and copy rows.

    With it running that long... do you have the two commands I initially commented out enabled or disabled? If it is deleting each row, we can change that up too which can make it run faster.

  11. #11
    Registered User
    Join Date
    09-12-2012
    Location
    Richmond, VA
    MS-Off Ver
    Varies but mostly 365
    Posts
    15

    Re: Macro to compare columns and copy rows.

    I'm still running tests. I've converted the variables and some of the structure to fit with the dataset in my report. I also noticed it was only copying 6 columns of data, my actual dataset has 36 which I think was your line:

    For j = 1 to 6

    So I changed it to:

    For j = 1 to 36

    Now I get an error when the 'Next J' loop completes

    Sub DataScrub()
    
        Dim wsNew As Worksheet, wsINC As Worksheet
        Dim lrow As Integer, nrow As Integer, i As Integer, j As Integer, rw As Integer
        Dim fndrng As Range
        Set wsNew = ThisWorkbook.Sheets("Page 1")
        Set wsINC = ThisWorkbook.Sheets("INCDATA")
        lrow = wsNew.Range("A:A").Find(what:="*", searchdirection:=xlPrevious).Row
        nrow = wsINC.Range("A:A").Find(what:="*", searchdirection:=xlPrevious).Row + 1
        
        i = 2
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
        Do While i <= lrow
            Set fndrng = wsINC.Range("A:A").Find(what:=wsNew.Cells(i, 1))
            If fndrng Is Nothing Then
                rw = nrow
                nrow = nrow + 1
            Else
                rw = fndrng.Row
            End If
            For j = 1 To 36
                wsINCDATA.Cells(rw, j) = wsNew.Cells(i, j)
            Next j
            wsNew.Rows(i).Delete
            lrow = lrow - 1
        Loop
        Application.ScreenUpdating = True
        Application.Calculation = xlCalculationAutomatic
    
    End Sub

  12. #12
    Valued Forum Contributor
    Join Date
    11-29-2022
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    435

    Re: Macro to compare columns and copy rows.

    You changed the name of the variable from wsINC to wsINCDATA in the line with:

    wsINCDATA.Cells(rw, j) = wsNew.Cells(i, j)

  13. #13
    Registered User
    Join Date
    09-12-2012
    Location
    Richmond, VA
    MS-Off Ver
    Varies but mostly 365
    Posts
    15

    Re: Macro to compare columns and copy rows.

    Yes, my apologies. I'm trying to juggle 10 things at work today while working on this code as well and pasted a previous iteration from my notes..


    Here is the direct copy paste from Excel's vba editor:

    Sub DataScrub()
    
        Dim wsNew As Worksheet, wsINC As Worksheet
        Dim lrow As Integer, nrow As Integer, i As Integer, j As Integer, rw As Integer
        Dim fndrng As Range
        Set wsNew = ThisWorkbook.Sheets("Page 1")
        Set wsINC = ThisWorkbook.Sheets("INCDATA")
        lrow = wsNew.Range("A:A").Find(what:="*", searchdirection:=xlPrevious).Row
        nrow = wsINC.Range("A:A").Find(what:="*", searchdirection:=xlPrevious).Row + 1
        
        i = 2
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
        Do While i <= lrow
            Set fndrng = wsINC.Range("A:A").Find(what:=wsNew.Cells(i, 1))
            If fndrng Is Nothing Then
                rw = nrow
                nrow = nrow + 1
            Else
                rw = fndrng.Row
            End If
            For j = 1 To 36
                wsINC.Cells(rw, j) = wsNew.Cells(i, j)
            Next j
            wsNew.Rows(i).Delete
            lrow = lrow - 1
        Loop
        Application.ScreenUpdating = True
        Application.Calculation = xlCalculationAutomatic
    
    End Sub
    With the above it's running through about 1100-1400 rows (of the 3400) then throwing an error in the j loop. I'm going to clear everything and try again with some new test data.

  14. #14
    Registered User
    Join Date
    09-12-2012
    Location
    Richmond, VA
    MS-Off Ver
    Varies but mostly 365
    Posts
    15

    Re: Macro to compare columns and copy rows.

    So wtih the above code, I cleared my data, imported a new update sheet and started the macro again.

    Run-time error '1004': Application-defined or object-defined error.

    Debug stops on: wsINC.Cells(rw, j) = wsNew.Cells(i, j)

    It processed about 500-550 rows before stopping.


    Could it be possible that it's failing when it finds a row in wsNew that doesn't exist in wsINC?
    Last edited by JasonMS; 12-16-2022 at 12:45 PM.

  15. #15
    Valued Forum Contributor
    Join Date
    11-29-2022
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    435

    Re: Macro to compare columns and copy rows.

    What are the values in rw, i, and j when it bugs? To find that, in the immediate window (CTRL+G if not visible) type ?rw,i,j and press enter.

  16. #16
    Registered User
    Join Date
    09-12-2012
    Location
    Richmond, VA
    MS-Off Ver
    Varies but mostly 365
    Posts
    15

    Re: Macro to compare columns and copy rows.

    lrow 3143
    nrow 2496
    rw 1436
    i 2
    j 29

    the INCDATA tab has 2495 rows (first is a header)
    the 'Page 1' tab has 3685 (also has a header)

    I've reset the sheet and imported the test data several times now and it's stopping in the same place with the same values for the variables you requested.

  17. #17
    Valued Forum Contributor
    Join Date
    11-29-2022
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    435

    Re: Macro to compare columns and copy rows.

    And how many rows are on wsINC?

  18. #18
    Valued Forum Contributor
    Join Date
    11-29-2022
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    435

    Re: Macro to compare columns and copy rows.

    That's very random. When it stops, what is the value in AC2? If you cannot post it for privacy, take a look at that cell. It's the one that Excel is upset over.

  19. #19
    Registered User
    Join Date
    09-12-2012
    Location
    Richmond, VA
    MS-Off Ver
    Varies but mostly 365
    Posts
    15

    Re: Macro to compare columns and copy rows.

    So this data comes from Incident records in ServiceNow... Column AC is an "Additional Comments" field it can contain several lines of text, depending on how much or how little the technician wanted to record. It can also contain full conversations that were copy/pasted from their Teams chat or lists of server devices several hundred lines long. It's something I'd like to keep but I can remove it from the report extract and use it in a different way.

    It is very possible that one of the 'Additional Comments' may be too large for the formula to handle?
    Last edited by JasonMS; 12-16-2022 at 03:02 PM.

  20. #20
    Registered User
    Join Date
    09-12-2012
    Location
    Richmond, VA
    MS-Off Ver
    Varies but mostly 365
    Posts
    15

    Re: Macro to compare columns and copy rows.

    Ok, on the INCDATA sheet Row 1436, Column AC contains a ton of information. One of my techs decided to write a book ...

    This could be part of the problem.

  21. #21
    Valued Forum Contributor
    Join Date
    11-29-2022
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    435

    Re: Macro to compare columns and copy rows.

    That is my thought. The value must be too long in length.

  22. #22
    Valued Forum Contributor
    Join Date
    11-29-2022
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    435

    Re: Macro to compare columns and copy rows.

    Can either skip that column entirely, something like

    If j <> 29 then
    Can also check the length. Not sure what the maximum is, but you can do something like

    If Len(wsNew.Cells(i, j)) < 255 then   'Again, no clue what the maximum is.

  23. #23
    Registered User
    Join Date
    09-12-2012
    Location
    Richmond, VA
    MS-Off Ver
    Varies but mostly 365
    Posts
    15

    Re: Macro to compare columns and copy rows.

    I will rework the report to remove column AC from being extracted in the first place. Thanks again for your help!!

  24. #24
    Valued Forum Contributor
    Join Date
    11-29-2022
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    435

    Re: Macro to compare columns and copy rows.

    Don't forget to adjust For j = 1 to 36 as necessary.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Macro to compare nominated columns to see if there are entries on the same rows
    By scottiex in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-03-2015, 05:31 AM
  2. [SOLVED] Macro to compare nominated columns to see if there are entries on the same rows
    By scottiex in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-02-2015, 07:09 PM
  3. [SOLVED] Excel macro, compare two columns from 2 sheets and copy the different rows
    By lagiosman in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-10-2013, 11:09 AM
  4. Compare two sheets,copy and paste unique rows based on values in 2 columns
    By ooggiemobile in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-02-2013, 03:58 AM
  5. Need to Compare Two Columns On Different Sheets and Copy Rows with Unique Value
    By triplesechumor in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-03-2013, 02:34 AM
  6. [SOLVED] Compare two rows and copy only changes from the columns
    By ahsanzafar in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-27-2012, 12:15 AM
  7. Need Macro to Compare Columns and then Reorganize Rows
    By WildSpreadsheets in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-12-2011, 11:03 PM

Tags for this Thread

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