合并一定数量的行

时间:2015-10-10 13:24:30

标签: excel vba excel-vba excel-formula excel-2010

我在Excel中有一个很大的名称列表,并希望将它们组合在一起。 例如:

 1. Mark
 2. Cindy
 3. Deb
 4. Mary
 5. Bill
 6. Roxy
 7. Bobby
 8. Richard

我希望它看起来像:

  1. Mark Cindy Deb Mary Bill Roxy Bobby Richard

请记住,我是在一个大型电子表格中执行此操作,因此我希望每个单元格中都有八个。

5 个答案:

答案 0 :(得分:0)

如果您拥有列“A”中的所有数据并希望将它们组合在列“B”中,那么您可以使用简单的宏代码将其分组为8,直到找不到空单元格。

Sub Macro1 ()    
i = 1
    Do Until Cells(i, 1) = ""
    Cells(i, 2) = Cells(i * 8 - 7, 1) & " " & Cells(i * 8 - 6, 1) & " " & Cells(i * 8 - 5, 1) & " " & Cells(i * 8 - 4, 1) & " " & _
        Cells(i * 8 - 3, 1) & " " & Cells(i * 8 - 2, 1) & " " & Cells(i * 8 - 1, 1) & " " & Cells(i * 8 - 0, 1)

    i = i + 1
    Loop
End sub

答案 1 :(得分:0)

假设Mark在A2中,请尝试在第2行中复制以适应:

=IF(INT((ROW()-2)/8)<>INT((ROW()-3)/8),OFFSET(A2,0,)&" "&OFFSET(A2,1,)&" "&OFFSET(A2,2,)&" "&OFFSET(A2,3,)&" "&OFFSET(A2,4,)&" "&OFFSET(A2,5,)&" "&OFFSET(A2,6,)&" "&OFFSET(A2,7,),"")  

如果适合你,那么你可能想要选择结果,复制,粘贴特殊......,值在顶部,然后整理。

答案 2 :(得分:0)

当你包括VBA我建议这个代码:

它的优点是可以设置快捷方式激活宏,用户可以在启动宏时提供列表的起始单元格。还可以选择在代码中对起始单元进行硬编码(参见代码中的注释)

Sub List_Combine_By()
Const kBy As Byte = 8
Dim CllSource As Range
Dim sNames As String
Dim lIn As Long, lOut As Long
Dim b As Byte

    Rem USE THIS LINE IF ACTIVATED BY A SHORTCUT
    Rem Users provide Starting Cell
    Set CllSource = Selection.Cells(1)

    Rem OTHERWISE USE THIS LINE TO DEFINE THE STARTING CELL
    'Set CllSource = Range("B3")

    With CllSource
        lOut = 0
        For lIn = 1 To .Worksheet.Rows.Count Step 8

            Rem Validate for Completness
            If .Cells(lIn, 1).Value2 = Empty Then Exit For

            Rem Clear Prior Results
            .Offset(lOut, 2).Resize(8, 1).ClearContents

            Rem Concatenate Names By
            sNames = Empty
            For b = 1 To kBy
                sNames = sNames & " " & .Cells(-1 + lIn + b, 1).Value2
            Next

            Rem Write Results
            .Offset(lOut, 2).Value = sNames
            lOut = 1 + lOut

    Next: End With

End Sub

答案 3 :(得分:0)

如果名称以A1开头,请从B1开始尝试并向下复制: -

=INDEX(A:A,ROWS(A1:A$1)*8-7)&" "&INDEX(A:A,ROWS(A1:A$1)*8-6)&" "&INDEX(A:A,ROWS(A1:A$1)*8-5)&" "&INDEX(A:A,ROWS(A1:A$1)*8-4)&" "&INDEX(A:A,ROWS(A1:A$1)*8-3)&" "&INDEX(A:A,ROWS(A1:A$1)*8-2)&" "&INDEX(A:A,ROWS(A1:A$1)*8-1)&" "&INDEX(A:A,ROWS(A1:A$1)*8)

答案 4 :(得分:-1)

你尝试过数组公式吗?听起来非常适合你的情况。将它与CONCATENATE相结合,很容易达到解决方案。

这里的一些示例:

https://support.office.com/en-us/article/Guidelines-and-examples-of-array-formulas-7d94a64e-3ff3-4686-9372-ecfd5caa57c7