VBA宏 合并单元格居中
时间: 2024-01-25 16:11:03 浏览: 122
VBA实现合并相同单元格
以下是VBA宏合并单元格并居中的代码:
```VBA
Sub mergeAndCenter()
Dim x As Long, y As Integer
x = Selection.Rows.Count
y = Selection.Columns.Count
a = Selection.Row
b = Selection.Column
Application.DisplayAlerts = False
For i = 1 To x
For j = 1 To y - 1
Cells(a + i - 1, b) = Cells(a + i - 1, b) & Cells(a + i - 1, b + j)
Next
Cells(a + i - 1, b).Resize(1, y).Select
Selection.Merge
Selection.HorizontalAlignment = xlHAlignCenter
Selection.VerticalAlignment = xlHAlignCenter
Next
Application.DisplayAlerts = True
End Sub
```
这个宏可以将选定区域内的所有单元格合并,并将合并后的单元格居中对齐。
阅读全文