如果将相同的行合并到不同的列中,则合并特定列中的单元格的宏 [英] Macro to merge cells in a specific column if same rows are merged in a different column

查看:88
本文介绍了如果将相同的行合并到不同的列中,则合并特定列中的单元格的宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助找到一个宏,该宏可以合并特定列中各行之间的单元格(如果这些行已合并到另一列中).下面是我现在拥有的屏幕截图,显示了需要基于已在列A中合并的单元格进行合并的列B中的单元格.

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

推荐答案

nixda已经回答了...

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

这篇关于如果将相同的行合并到不同的列中,则合并特定列中的单元格的宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆