合并第1行中的每3行

时间:2016-12-08 15:40:02

标签: excel vba excel-vba excel-2013

我正在尝试合并第1行的每3个单元格(从B1开始,最后要合并的单元格是FY - 意味着FW,FX& FY应该合并。)。我已经使用它来合并列中的每3行,但是我如何更改它以跨越第1行?

Function MergeHeaders()
Application.DisplayAlerts = False

Dim RgToMerge As Range

For i = 3 To ActiveSheet.Cells(Rows.Count, 3).End(xlUp).Row Step 3

Set RgToMerge = Range(Cells(i, 3), Cells(i + 1, 3))
With RgToMerge
    .Merge
    .HorizontalAlignment = xlCenterAcrossSelection
    .VerticalAlignment = xlCenter
End With

Next i
End Function

1 个答案:

答案 0 :(得分:3)

更像这样的东西?

Function MergeHeaders()
Dim RgToMerge As Range

  Application.DisplayAlerts = False

  For i = 2 To ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column Step 3
    Set RgToMerge = Range(Cells(1, i), Cells(1, i + 2))
    With RgToMerge
      .Merge
      .HorizontalAlignment = xlCenterAcrossSelection
      .VerticalAlignment = xlCenter
    End With
  Next i
End Function