EXCEL:如何合并具有特定值的单元格

时间:2014-11-01 09:18:26

标签: excel

我在Excel上有这个表

http://i.stack.imgur.com/N2kDS.png

我希望看起来像这样,但要为所有列执行此操作,因为我有很多行。

http://i.stack.imgur.com/1v0TE.png

1 个答案:

答案 0 :(得分:0)

1-保存启用宏的电子表格。 (另存为.xlsm) 2-打开视图选项卡,单击宏,查看宏并进行编辑 3-粘贴以下代码:

Sub Macro0()
    'I assume your information is in the column A
    With Range("A1:A65000")

    'I also assume all CATEGORIES start with the word CATEGORY
    'If that is not the case you have to define a list with all GATEGORIES
    Set c = .Find("GATEGORY", LookIn:=xlValues)

    If Not c Is Nothing Then
        firstAddress = c.Address
        oneBefore = firstAddress - 1
        Do
            Range("A1", oneBefore).Select
            Selection.Merge
            Set c = .FindNext(c)
        Loop While Not c Is Nothing And c.Address <> firstAddress
    End If
    End With

End Sub

请注意我在代码中的评论。 让我知道它是否适合你。

来源:http://msdn.microsoft.com/en-us/library/office/ff839746%28v=office.15%29.aspx