【发布时间】:2013-10-15 12:52:47
【问题描述】:
我在 excel 中有一个小问题。我没有使用 excel 宏的经验,如果能得到一些帮助,我将不胜感激。我正在尝试找到一个宏来调整合并单元格的高度以适合其内容。自动地。我发现可以对多列中的单元格执行此操作,但不能对多行执行此操作,也不能自动执行此操作:
Sub AutoFitMergedCellRowHeight()
Dim CurrentRowHeight As Single, MergedCellRgWidth As Single
Dim CurrCell As Range
Dim ActiveCellWidth As Single, PossNewRowHeight As Single
Dim iX As Integer
If ActiveCell.MergeCells Then
With ActiveCell.MergeArea
If .Rows.Count = 1 And .WrapText = True Then
Application.ScreenUpdating = False
CurrentRowHeight = .RowHeight
ActiveCellWidth = ActiveCell.ColumnWidth
For Each CurrCell In Selection
MergedCellRgWidth = CurrCell.ColumnWidth + _
MergedCellRgWidth
iX = iX + 1
Next
MergedCellRgWidth = MergedCellRgWidth + (iX - 1) * 0.71
.MergeCells = False
.Cells(1).ColumnWidth = MergedCellRgWidth
.EntireRow.AutoFit
PossNewRowHeight = .RowHeight
.Cells(1).ColumnWidth = ActiveCellWidth
.MergeCells = True
.RowHeight = IIf(CurrentRowHeight > PossNewRowHeight, _
CurrentRowHeight, PossNewRowHeight)
End If
End With
End If
结束子
最终结果应该是这样的: 提前谢谢你。
【问题讨论】:
-
您应该能够对行遵循类似的模式:如果合并单元格然后取消合并它们,自动调整第一个单元格并注意适合的行高。然后重新合并单元格并将最后一个单元格的高度设置为您记下的高度减去所有其他行的高度。