合并并居中多行

时间:2015-12-08 08:43:01

标签: excel vba excel-vba

如何在Excel中合并和居中多行?

例如,我想合并并将行A1和A2,B1和B2,C1& C2,F1& F2和A3& A4,B3& B4,C3& C4 ... F3& F4等合并到A902& 903 .. F902..903

2 个答案:

答案 0 :(得分:0)

见下面的代码:

Sub SomeSub()

    Dim Range1 As Range
    'Looping through the rows steppin up by two each time
    For r = 1 To 902 Step 2

        'Looping through the Columns
        For col = 1 To 6

            'Setting the range for the two cells
            Set Range1 = Range(Cells(r, col), Cells(r + 1, col))
            'Merging the cells
            Range1.Merge
            'Setting horizontal alignment
            Range1.HorizontalAlignment = xlCenter

        Next col
    Next r

End Sub

请注意,该脚本将合并行901和902,这些行合并第1行和第2行,每次加2。如果要合并行903和904,则将902更改为904。

答案 1 :(得分:-1)

将单元格A1与A2合并,然后自动填充整个范围的格式(时间~5-7秒)