+ Reply to Thread
Results 1 to 6 of 6

Merging Duplicate Rows and Keeping Original Data

Hybrid View

  1. #1
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,835

    Re: Merging Duplicate Rows and Keeping Original Data

    You could use a macro like:
    Sub Merge_DeleteDuplicateRows()
    Application.ScreenUpdating = False
    Dim LRow As Long, i As Long, j As Long, k As Long
    With ActiveSheet
      LRow = .Range("A" & .Cells.SpecialCells(xlCellTypeLastCell).Row).End(xlUp).Row
      For i = 14 To LRow
        For j = LRow To i + 1 Step -1
          If .Range("A" & i).Value = .Range("A" & j).Value Then
            For k = 4 To 9
              If .Cells(j, k).Value <> 0 Then .Cells(i, k).Value = .Cells(j, k).Value + .Cells(i, k).Value
            Next
            .Range("A" & j).EntireRow.Delete
            LRow = LRow - 1
          End If
        Next
      Next
    End With
    Application.ScreenUpdating = True
    End Sub
    However, I also note that your original post had multiple entries for Carl Carlisle, with different therapists, but the data you wanted merged didn't indicate which therapist's records should be updated. As coded, the above macro will merge & delete the last of those lines also. To avoid that, some extra code would be required, for testing whether there's anything in the therapist column and to avoid both sets of records being updated. For proper control, you'd need to indicate the therapist details for every one of the merge row candidates, so they too can be assigned to the correct therapists. That said, the following code additions after 'For j = LRow To i + 1 Step -1' will prevent duplications and the merging of the therapist records:
          If .Range("A" & i).Value = .Range("A" & i + 1).Value Then Exit For
          If .Range("B" & j).Value <> "" Then Exit For
    Using ' i + 1' populates the last therapist record, whilst ' i - 1' populates the first therapist record.
    Last edited by macropod; 02-19-2014 at 02:08 AM. Reason: Additional content
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  2. #2
    Registered User
    Join Date
    02-18-2014
    Location
    Lebanon, NH
    MS-Off Ver
    Excel for Mac 2011
    Posts
    3

    Re: Merging Duplicate Rows and Keeping Original Data

    Great, thank you so much! I'll run some tests over the next couple days and let you know.

+ 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. Replies: 1
    Last Post: 01-22-2013, 09:17 AM
  2. Replies: 3
    Last Post: 01-18-2012, 03:36 AM
  3. Merging duplicate rows whilst retaining some unique data (examples attached)
    By BaronDJB in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-31-2011, 08:06 AM
  4. [SOLVED] Copying data but keeping original
    By mellowe in forum Excel General
    Replies: 2
    Last Post: 01-12-2006, 10:25 AM
  5. Keeping original data format into a Pivot Table
    By carim.fam@wanadoo.fr in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-10-2005, 09:06 AM

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