+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 26

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

  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 08:34 AM. Reason: code provided worked perfectly!

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

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

  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 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
    You're welcome...glad I could help.
    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. #5
    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?

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

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

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

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

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

  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

    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.

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

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

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

  15. #15
    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?

+ 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