Merge duplicates with cell color

neulite

New Member
Joined
Feb 20, 2018
Messages
3
I have a list of duplicate cases (column A). Both duplicates have information that the other may or may not have. I would like to somehow merge the duplicate cases and return the non empty value corresponding to the attribute. Additionally, I would like to carry over the cell color (pretend the number colors below are the cell fill colors). Is this possible? See below


Original data
CaseAttribute1Attribute2Attribute3Attribute4Attribute5
136(blank)3AO3
1(blank)apple3AO(blank)
2(blank)orange(blank)(blank)85
261(blank)3AD(blank)
354orange(blank)BL40
3(blank)(blank)4(blank)(blank)

<tbody>
</tbody>


Output I am looking for
CaseAttribute1Attribute2Attribute3Attribute4Attribute5
136apple3AO3
261orange3AD85
354orange4BL40

<tbody>
</tbody>
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Take a copy of the sheet and then try running this macro:

Code:
Public Sub MergeCaseRows()

Dim lastRow As Long
Dim thisRow As Long
Dim lastCol As Long
Dim thisCol As Long

' Find the last row of data
lastRow = Cells(Rows.Count, "A").End(xlUp).Row

' Start on row 3
thisRow = 3

' Keep going until the end
Do While thisRow <= lastRow
    ' Is this case the same as the previous row?
    If Cells(thisRow, "A").Value = Cells(thisRow - 1, "A").Value Then
        ' Find the last column
        lastCol = Cells(thisRow, Columns.Count).End(xlToLeft).Column
        
        ' Process all columns
        For thisCol = 2 To lastCol
            ' Check if the previous row is empty
            If IsEmpty(Cells(thisRow - 1, thisCol).Value) Then
                ' Copy this row
                Cells(thisRow, thisCol).Copy Destination:=Cells(thisRow - 1, thisCol)
            End If
        Next thisCol
        
        ' Delete this duplicate row
        Cells(thisRow, "A").EntireRow.Delete
        
        ' We've lost a row from the sheet
        lastRow = lastRow - 1
    Else
        ' Move to the next row
        thisRow = thisRow + 1
    End If
Loop

End Sub

WBD
 
Upvote 0
Take a copy of the sheet and then try running this macro:

Code:
Public Sub MergeCaseRows()

Dim lastRow As Long
Dim thisRow As Long
Dim lastCol As Long
Dim thisCol As Long

' Find the last row of data
lastRow = Cells(Rows.Count, "A").End(xlUp).Row

' Start on row 3
thisRow = 3

' Keep going until the end
Do While thisRow <= lastRow
    ' Is this case the same as the previous row?
    If Cells(thisRow, "A").Value = Cells(thisRow - 1, "A").Value Then
        ' Find the last column
        lastCol = Cells(thisRow, Columns.Count).End(xlToLeft).Column
        
        ' Process all columns
        For thisCol = 2 To lastCol
            ' Check if the previous row is empty
            If IsEmpty(Cells(thisRow - 1, thisCol).Value) Then
                ' Copy this row
                Cells(thisRow, thisCol).Copy Destination:=Cells(thisRow - 1, thisCol)
            End If
        Next thisCol
        
        ' Delete this duplicate row
        Cells(thisRow, "A").EntireRow.Delete
        
        ' We've lost a row from the sheet
        lastRow = lastRow - 1
    Else
        ' Move to the next row
        thisRow = thisRow + 1
    End If
Loop

End Sub

WBD

Thank you. Unfortunately, this is the output I get with that code:

Case

<tbody>
</tbody>
Attribute1

<tbody>
</tbody>
Attribute2

<tbody>
</tbody>
Attribute3

<tbody>
</tbody>
Attribute4

<tbody>
</tbody>
Attribute5

<tbody>
</tbody>
1

<tbody>
</tbody>
36

<tbody>
</tbody>
(blank)

<tbody>
</tbody>
3

<tbody>
</tbody>
AO

<tbody>
</tbody>
3

<tbody>
</tbody>
2

<tbody>
</tbody>
(blank)

<tbody>
</tbody>
orange

<tbody>
</tbody>
(blank)

<tbody>
</tbody>
(blank)

<tbody>
</tbody>
85

<tbody>
</tbody>
3

<tbody>
</tbody>
54

<tbody>
</tbody>
orange

<tbody>
</tbody>
(blank)

<tbody>
</tbody>
BL

<tbody>
</tbody>
40

<tbody>
</tbody>

<tbody>
</tbody>

It seems to have retained the blank rows instead of going to the first non-blank cell.

I opened up the editor, pasted the code and saved. Then alt-F8 and run on the tab with the data. Am I doing it wrong?
 
Upvote 0
Does it actually say "(blank)" in those cells or are they just empty? I assumed they were empty.

WBD
 
Upvote 0

Forum statistics

Threads
1,216,031
Messages
6,128,420
Members
449,449
Latest member
Quiet_Nectarine_

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top