+ Reply to Thread
Results 1 to 11 of 11

Identify duplicate rows and copy/Paste the first cell to all cells of equal rows

  1. #1
    Registered User
    Join Date
    04-14-2014
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    6

    Identify duplicate rows and copy/Paste the first cell to all cells of equal rows

    Hi All,
    I am new to macro and just trying to learn. I have a spreadsheet with 20000 rows and 8 columns. I am trying to identify equal rows based on the values of columns C, D, E and F. then I need to separate equal bunches with a blank row. Then I need to copy the ID number from the first cell of column B of each bunch and paste it for the rest of the rows in that bunch. I have written the following code but this does not put the ID of the first cell in a bunch for the rest of the rows in that bunch. Can anyone help me out with this Please?

    Here is my code:


    Please Login or Register  to view this content.

    Cheers;
    Last edited by arlu1201; 04-14-2014 at 07:45 AM. Reason: Use code tags.

  2. #2
    Forum Contributor
    Join Date
    02-28-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    187

    Re: Identify duplicate rows and copy/Paste the first cell to all cells of equal rows

    As a general tip (and certainly something I find useful, although others may disagree), unless you plan on moving the order of the columns around, refer to both row and column with an integer value.

    Then, when referring to relative cells, you need not use offset, but you could (for example if referring to the cell 1 to the left and one up from D2)

    Please Login or Register  to view this content.
    It might also help if you assign your last-row calculation to a variable and then loop through 'i' (as you are not in fact looping through your last-row, that is constant), i.e.

    Please Login or Register  to view this content.
    As for inserting a new row, you can trim the code down to:

    Please Login or Register  to view this content.
    Inserting the ID, if you apply the ID value to a string (or integer, depending) Then loop from 1 - 8 (i.e. A-E) and set each row 'heading' (i.e. each blank row) cell value to equal your 'B' value.
    Last edited by TKCZBW; 04-14-2014 at 08:32 AM. Reason: Spelling error

  3. #3
    Registered User
    Join Date
    04-14-2014
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Identify duplicate rows and copy/Paste the first cell to all cells of equal rows

    thanks for the reply. good tips. very useful. however, I'm still struggling to copy the first ID of each bunch and paste for all the other cells in that bunch. E.g. if there are four equal rows with four different IDs in column B, then I want the B1 to be copied to B2, B3 and B4. same should be repeated for all the separated rows. E.g. 20000 lines could be separated into 150 bunches where each bunch contains equal rows and then the first cell of each bunch in column B will be pasted into the rest of the cells of column B of that bunch.

  4. #4
    Forum Contributor
    Join Date
    02-28-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    187

    Re: Identify duplicate rows and copy/Paste the first cell to all cells of equal rows

    Aha. I see what you mean.

    How about:

    Please Login or Register  to view this content.
    If you put this loop after the first loop (i.e. the one inserting the blank rows) then it should cycle through change all 'B' values within a group to the first 'B' value of that group.

    Hopefully should work.

  5. #5
    Registered User
    Join Date
    04-14-2014
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Identify duplicate rows and copy/Paste the first cell to all cells of equal rows

    Thanks TKCZBW. I'm going to give it a try. will let you know how it went.

    cheers

  6. #6
    Forum Contributor
    Join Date
    02-28-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    187

    Re: Identify duplicate rows and copy/Paste the first cell to all cells of equal rows

    I've just spotted an error in my code, if you replace (on line 5) the first 'i' with i+1 and then the 'i+1' with i, that should (fingers crossed) do it.

  7. #7
    Registered User
    Join Date
    04-14-2014
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Identify duplicate rows and copy/Paste the first cell to all cells of equal rows

    Hi TKCZBW. thanks again for your input. I have tried to use your method. however, it gives me the same results as my original code, above, did. it copies B2 to A3 then B3 to A4 and so on. what I need is to copy the B2 to A2, A3, and so on as long as the rows are equal based on columns D,E,F,G. below is the new code based on your inputs. I've also included a spread sheet example of what the code does on sheet 1 and what I need on sheet 2. Many thanks

    Sub MIdCopyNew()
    Dim lrow As Long


    lrow = ActiveSheet.UsedRange.Rows.Count

    For i = lrow To 2 Step -1



    If Cells(i, 4).Value = Cells(i + 1, 4).Value And Cells(i, 5).Value = Cells(i + 1, 5).Value And Cells(i, 6).Value = Cells(i + 1, 6).Value And Cells(i, 7).Value = Cells(i + 1, 7).Value Then ' and rows D,E and so on here as well in an 'and' statment)

    If Cells(i + 1, 2).Value <> "" Then 'Skip blank rows by sampling column 'C'
    Cells(i + 1, 1).Value = Cells(i, 2).Value
    End If
    End If

    Next i


    End Sub


    What the Code Does:
    What Code Does.jpg

    What I need it to do:
    What I need it to do.jpg

  8. #8
    Forum Contributor
    Join Date
    02-28-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    187

    Re: Identify duplicate rows and copy/Paste the first cell to all cells of equal rows

    Ok, so you want each data block to have the same new primary value as the first recorded ID value within that block?

    If the code is correctly inserting a new row to deliniate the blocks (which doesn't appear on your examples, but was in your initial query) then I would:

    Please Login or Register  to view this content.
    Let me know how it works.

  9. #9
    Registered User
    Join Date
    04-14-2014
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Identify duplicate rows and copy/Paste the first cell to all cells of equal rows

    Perfect TKCZBW. much appreciated. it works just fine. Brilliant.

    cheers

  10. #10
    Forum Contributor
    Join Date
    02-28-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    187

    Re: Identify duplicate rows and copy/Paste the first cell to all cells of equal rows

    Good! Sorry it took me so long to understand the question! If you could mark the question as answered and add reputation, that would be most appreciated!

  11. #11
    Registered User
    Join Date
    04-14-2014
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Identify duplicate rows and copy/Paste the first cell to all cells of equal rows

    Sure. No worries. It's already done. !

    thanks again

+ 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. Copy cell values (not formulas) from a pair of rows then paste in a new set of rows
    By pelle3988 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-23-2013, 02:49 PM
  2. copy paste usedrange data and delete duplicate rows
    By mani_bbc05 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-09-2013, 02:12 PM
  3. Replies: 6
    Last Post: 03-04-2013, 12:03 AM
  4. Replies: 0
    Last Post: 10-16-2012, 03:50 AM
  5. Replies: 1
    Last Post: 05-23-2012, 03:02 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