*HELP* Merge rows if 2 cells partially match

L

Legacy 171839

Guest
Hi,

Proper stuck here.

Problem - Need some vba to do the following:

Criteria: Identify if 1st 12 characters in A1 match 1st 12 characters in A2.
Action: If they do, merge rows 1 and 2 (data wont overlap; b1 may have data, c2 may have data. We want to keep both in merged row. B1 and B2 will never both contain data).
Code should look down the whole of Column A and merge all rows where the cell 1st 12 characters of the cell match the 1st 12 of cell directly below it.
Hope this makes sense, and really hope you can help.

Thanks in advance
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Can there only ever be 2 rows that you want to merge? Wouldn't it be better to have the data on only 1 row, deleting the second?
 
Upvote 0
Can there only ever be 2 rows that you want to merge? Wouldn't it be better to have the data on only 1 row, deleting the second?

To explain - may look like this

Excel12345AB 40 -- 60 90
Excel12345AC -- 50 -- --

We would want the above to look like

Excel 12345 40 60 50 90

Both rows merged to one. Hope thats clearer...
 
Upvote 0
Try:

Code:
Sub Test()
    Dim r As Long
    Dim c As Long
    With ActiveSheet.Range("a1").CurrentRegion
        For r = .Rows.Count To 2 Step -1
            If Left(.Cells(r, 1).Value, 12) = Left(.Cells(r - 1, 1).Value, 12) Then
                For c = 2 To .Columns.Count
                    If .Cells(r, c).Value <> "" Then
                        .Cells(r - 1, c).Value = .Cells(r, c).Value
                    End If
                Next c
                .Rows(r).Delete Shift:=xlUp
            End If
        Next r
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,241
Members
452,622
Latest member
Laura_PinksBTHFT

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