Macro to merge cells in a specific column if same

2019-09-07 02:37发布

I need help finding a macro that can merge cells across rows in a specific column if those same rows are already merged in another column. Below is a screenshot of what I have now that shows the cells in Column B that need to be merged based on cells that are already merged in Column A.

http://i.stack.imgur.com/bRerd.jpg

The below screenshot is what I need the spreadsheet to look like after the macro runs

http://i.stack.imgur.com/Jm4ve.jpg

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-09-07 03:28

nixda already answered it...

Sub mergecolumn()

Dim cnt As Integer
Dim rng As Range
Dim str As String

For i = ActiveSheet.UsedRange.Rows.Count To 1 Step -1
cnt = Cells(i, 1).MergeArea.Count
Set rng = Range(Cells(i, 2), Cells(i - cnt + 1, 2))

For Each cl In rng
    If Not IsEmpty(cl) Then str = str + vbNewLine + cl
Next
If str <> "" Then str = Right(str, Len(str) - 2)

Application.DisplayAlerts = False
rng.Merge
rng = str
Application.DisplayAlerts = True

str = ""
i = i - cnt + 1
Next i

End Sub
查看更多
登录 后发表回答