通过 Excel VBA 设置行高,并带有合并单元格和自动换行

通过 Excel VBA 设置行高,并带有合并单元格和自动换行

哪个 Excel VBA 代码可以设置正确的行高(所有字符可见且没有多余的空格),并启用合并单元格和自动换行。我的字体是 Calibri 11。

在启用自动换行的情况下,字体大小、列大小和行高之间是否存在某种简单的关系?

答案1

从 OP 的问题中移出的答案:

OrigRowHeight = SafeRange.RowHeight
OrigColWidth = SafeRange.ColumnWidth

CurRow = ActiveCell.Row
CurCol = ActiveCell.Column
NumMergeCols = ActiveCell.MergeArea.Count
LastCol = CurCol + NumMergeCols - 1

For i = CurCol To LastCol
    CombinedColWidth = CombinedColWidth + Cells(CurRow, i).ColumnWidth
Next i

' Most of the following code came from Superuser user6261023 (My Thanks)
With SafeSheet.Range(SafeRange.Address)
    TargetRange.Copy
    .PasteSpecial xlPasteAll
    .UnMerge
    .ColumnWidth = CombinedColWidth
    .Value = TargetRange.Value
    .EntireRow.AutoFit
    NeededRowHeight = 1.05 * .RowHeight / TargetRange.MergeArea.Rows.Count
    .ClearContents
    .ClearFormats
    .RowHeight = OrigRowHeight
    .ColumnWidth = OrigColWidth
End With

'Return NeededRowHeight
NewRowHeight = NeededRowHeight

相关内容