我试图合并第 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

最佳答案

更像这样的东西?

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

关于vba - 在第 1 行中每 3 行合并一次,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41043254/

10-12 21:30