Sub MergeCell_AutoHeight()
Dim rh As Single, mw As Single
Dim rng As Range, rrng As Range, n1%, n2%
Dim aw As Single, rh1 As Single
Dim m$, n$, k
Dim ir1, ir2, ic1, ic2
Sheet2.Activate
Application.ScreenUpdating = False
For Each rrng In ActiveSheet.UsedRange
If rrng.Address <> rrng.MergeArea.Address Then
If rrng.Address = rrng.MergeArea.Item(1).Address Then
rrng.Activate
m = Split(rrng.MergeArea.Address, ":")(0)
n = Split(rrng.MergeArea.Address, ":")(1)
ir1 = Split(m, "$")(2)
ic1 = Split(m, "$")(1)
ir2 = Split(n, "$")(2)
ic2 = Split(n, "$")(1)
With ActiveCell.MergeArea
.WrapText = True
If .Rows.Count = 1 Then
aw = ActiveCell.ColumnWidth
For Each rng In Selection
mw = mw + rng.ColumnWidth
Next
.MergeCells = False
.Cells(1).ColumnWidth = mw
.EntireRow.AutoFit
rh1 = .RowHeight
.Cells(1).ColumnWidth = aw
.MergeCells = True
.RowHeight = rh1
mw = 0
ElseIf .Columns.Count = 1 Then
.MergeCells = False
.EntireRow.AutoFit
rh1 = .Cells(1).RowHeight
n1 = Selection.Rows.Count
For Each rng In Selection
rng.RowHeight = rh1 / n1
Next
.MergeCells = True
Else
aw = ActiveCell.ColumnWidth
n1 = Selection.Rows.Count
n2 = Selection.Columns.Count
For Each rng In Selection
mw = mw + rng.ColumnWidth
k = k + 1
If k = n2 Then
Exit For
End If
Next
k = 0
.MergeCells = False
.Cells(1).ColumnWidth = mw
.EntireRow.AutoFit
rh1 = .Cells(1).RowHeight
.Cells(1).ColumnWidth = aw
For i = ir1 To ir2
ActiveSheet.Cells(i, ic1).RowHeight = rh1 / n1
Next
.MergeCells = True
mw = 0
End If
End With
End If
End If
Next
Application.ScreenUpdating = True
End Sub
VBA-autofit
答案:2 悬赏:0 手机版
解决时间 2021-04-12 15:28
- 提问者网友:浩歌待明月
- 2021-04-12 11:05
最佳答案
- 五星知识达人网友:雪起风沙痕
- 2021-04-12 12:37
Excel的单元格在合并后,失去了自动调整行高的功能。这段代码就是调整行高,把没有显示的字都显示出来。
全部回答
- 1楼网友:行路难
- 2021-04-12 13:24
那是因为那一列启用了自动换行的功能,所以自动调整不起作用,可以先将这列的列宽设置为一个非常大的值,然后于使用autofit即可.
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯