Macro to merge cells in a specific column if same rows are merged in a different column

痞子三分冷 提交于 2019-12-11 21:23:32

问题


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.

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


回答1:


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


来源:https://stackoverflow.com/questions/17308355/macro-to-merge-cells-in-a-specific-column-if-same-rows-are-merged-in-a-different

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!