這對我有用。它通過調整最後一個單元格的行高來調整第一列的合併單元格的大小,否則文本將被截斷。
稍微複雜一點的版本可能會劃分合併區域中所有行之間的高度差,而不是將其添加到單個行中。我可以離開,作爲一個練習...
Sub FixHeights()
Dim rng As Range, col As Range, m As Range, c As Range
Dim i As Long, n As Long, fh
Dim fHeights()
Set rng = Range("B4:C11") 'for example...
'to store merged areas and their fitted heights
ReDim fHeights(1 To rng.Rows.Count, 1 To 2)
'run though the first column and find merged
' areas and "fitted heights"
Set col = rng.Columns(1)
n = 0
For Each c In col.Cells
Set m = c.MergeArea
If m.Cells.Count > 1 And c.Row = m.Cells(1).Row Then
n = n + 1
Set fHeights(n, 1) = m
fHeights(n, 2) = GetFittedHeight(m)
End If
Next c
'autofit the second column row heights
rng.Columns(2).Rows.AutoFit
'recheck the first column: if any merged area is
' too short, then increase the last row's height
For i = 1 To n
Set m = fHeights(i, 1)
fh = fHeights(i, 2)
Debug.Print m.Height, fh
If m.Height < fh Then
With m.Cells(m.Cells.Count)
.RowHeight = .RowHeight + (fh - m.Height)
End With
End If
Next i
End Sub
'get the "fitted height" of a merged area
Function GetFittedHeight(ma As Range)
Dim ht
With ma
.UnMerge
.Cells(1).EntireRow.AutoFit
ht = .Cells(1).RowHeight
.Merge
.EntireRow.AutoFit
End With
GetFittedHeight = ht
End Function
在這裏看到一些有用的代碼 - http://blog.contextures.com/archives/2012/06/07/autofit-merged-cell-row-height / –