Excel 如果数据相同,是否合并两行特定列?
我有如下输入 我需要输出如下 基本上,如果数据与公式相同,如何仅合并列A的相邻值?这是一张巨大的床单。我想应用公式,使任务自动化Excel 如果数据相同,是否合并两行特定列?,excel,merge,Excel,Merge,我有如下输入 我需要输出如下 基本上,如果数据与公式相同,如何仅合并列A的相邻值?这是一张巨大的床单。我想应用公式,使任务自动化 那么你尝试了什么?你在问什么?@ScottCraner更新了我的帖子。公式无法做到这一点,你需要vba代码。你可能需要注意数据必须在A列排序,否则可能会合并错误数量的单元格,这段代码将合并第一个实例和最后一个实例之间的所有内容,无论它们之间是否有其他文本。@johnyL谢谢。你能简单地解释一下吗。我来自爪哇。我在上面的代码中没有看到我们比较第1列数据的地方?@Sco
那么你尝试了什么?你在问什么?@ScottCraner更新了我的帖子。公式无法做到这一点,你需要vba代码。你可能需要注意数据必须在A列排序,否则可能会合并错误数量的单元格,这段代码将合并第一个实例和最后一个实例之间的所有内容,无论它们之间是否有其他文本。@johnyL谢谢。你能简单地解释一下吗。我来自爪哇。我在上面的代码中没有看到我们比较第1列数据的地方?@ScottCraner根据OP的样本判断,数据在第
A列
:)我将指出“中间的文本”。@JohnyL从未质疑数据的位置,只是OP必须在运行代码之前对A列进行排序,以确保正确合并。否则,最好使用查找。我不会想到这一点,我会使用Application.Countif
。你的会更快。@ScottCraner是的,我同意你的意见,使用Find
时必须小心。需要确保不会有间隙或额外的数据潜入。:)
Sub G()
Dim x&, start&, bottom&
Application.DisplayAlerts = False
'// Start from row two
'// In your sample it's value "Text1"
x = 2
While Not IsEmpty(Cells(x, 1))
'// "start" designates the start row with new text (see below)
start = x
'// As soon as we defined start row with new text, it's time to define the last row.
'// Cells(x, 1) - is new text value
'// SearchDirection:=xlPrevious means that search must be done from bottom of sought range
'// This also means that no extra data must be under your data.
'// So, this way we can get to know full range.
'// For single distinct value the start and bottom will be the same row.
bottom = Range("A:A").Find(Cells(x, 1), SearchDirection:=xlPrevious).Row
'// Having start and bottom rows,
'// we can get full range with new text by using Resize property.
'// It sets new number of rows and columns having top-left cell as a start point.
Cells(start, 1).Resize(bottom - start + 1).Merge
'// The next cell after bottom cell is new value.
x = bottom + 1
Wend
Application.DisplayAlerts = True
End Sub