【问题标题】:Excel autofit row height doesn't work on meged cells with word wrapExcel 自动调整行高不适用于带有自动换行的合并单元格
【发布时间】:2018-02-01 02:37:28
【问题描述】:

我正在以编程方式将一些文本连续插入到合并的单元格中。我设置了换行文本,并希望根据需要扩展行高以容纳多行文本。填充单元格后,我以编程方式应用 AutoFit,但这不起作用。我随后发现一篇知识库文章说 AutoFit 不适用于合并的单元格!我可以尝试计算容纳换行文本行数所需的行高。但我真的不想爬进计算字符宽度等。任何想法都非常感谢。

问题归功于大卫(我有完全相同的问题,只是为了后代转发)source

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    我找到了一个 VB 宏 here,它将模拟活动工作表上任何合并单元格的自动调整。来自 MrExcel.com 的来源学分招架

    Sub AutoFitMergedCellRowHeight()
    Dim CurrentRowHeight As Single, MergedCellRgWidth As Single
    Dim CurrCell As Range
    Dim ActiveCellWidth As Single, PossNewRowHeight As Single
    Dim StartCell As Range, c As Range, MergeRng As Range, Cell As Range
    Dim a() As String, isect As Range, i
    
    
    'Take a note of current active cell
    Set StartCell = ActiveCell
    
    'Create an array of merged cell addresses that have wrapped text
    For Each c In ActiveSheet.UsedRange
    If c.MergeCells Then
        With c.MergeArea
        If .Rows.Count = 1 And .WrapText = True Then
            If MergeRng Is Nothing Then
                Set MergeRng = c.MergeArea
                ReDim a(0)
                a(0) = c.MergeArea.Address
            Else
            Set isect = Intersect(c, MergeRng)
                If isect Is Nothing Then
                    Set MergeRng = Union(MergeRng, c.MergeArea)
                    ReDim Preserve a(UBound(a) + 1)
                    a(UBound(a)) = c.MergeArea.Address
                End If
            End If
        End If
        End With
    End If
    Next c
    
    
    Application.ScreenUpdating = False
    
    'Loop thru merged cells
    For i = 0 To UBound(a)
    Range(a(i)).Select
          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
                    Next
                    .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
    MergedCellRgWidth = 0
    Next i
    
    StartCell.Select
    Application.ScreenUpdating = True
    
    'Clean up
    Set CurrCell = Nothing
    Set StartCell = Nothing
    Set c = Nothing
    Set MergeRng = Nothing
    Set Cell = Nothing
    
    End Sub
    

    【讨论】:

    • 提醒任何打算使用此功能的人:如果您将单元格合并到多行,这将不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-31
    • 2019-08-03
    相关资源
    最近更新 更多