Excelmacros:如何扩展行高以适应包装文本?

我正在慢慢地修改和扩展If … ElseIf … Else语句( 请参阅post )来帮助我格式化一长串类别和子类别(谢谢marg&Lunatik)。

我已经将固定行高分配到范围/行的90%。 现在我被困在那些有很多文字的单元格中,这些文字包装在单元格中的两行中。 两行文字不符合我的10.5标准高度。

我不能简单地刷新屏幕,因为声明说任何不是例外的(粗体) ,或者例外two(上标)应该是10.5pts。 我需要第三个例外 。 我目前有:

Sub setHeights() Dim targetRange As Range Dim targetCell As Range Cells.Select Selection.WrapText = True Cells.EntireRow.AutoFit Set targetRange = Range("B:B") For Each targetCell In targetRange If Not IsEmpty(targetCell) Then If targetCell.Font.Bold Then targetCell.RowHeight = 15 ElseIf targetCell.Characters(Len(targetCell), 1).Font.superscript Then targetCell.RowHeight = 14 Else: targetCell.RowHeight = 10.5 End If End If Next targetCell End Sub 

我可否:

  • find超过60个字符的目标细胞(固定列的宽度)
  • 将.WrapText应用于这些特定的targetCells
  • AutoExpand只有那些targetCells(因此不会覆盖我的标准10.5pt线为其他非例外targetCells)。

这会工作吗? 它是否需要放置在一个单独的子例程中,因为第一个参数? 这究竟是什么样子? (请看下面的我的尴尬的工作)

 ElseIf targetCell.Characters(Len(TargetCell+60).TargetCell.WrapText Then targetCell.Autofit 

这似乎工作。

 Sub setHeights() Dim targetRange As Range Dim targetCell As Range Set targetRange = Range("B:B") For Each targetCell In targetRange.Cells If Not IsEmpty(targetCell.Value) Then If targetCell.Font.Bold Then targetCell.RowHeight = 15 ElseIf targetCell.Characters(Len(targetCell), 1).Font.Superscript Then targetCell.RowHeight = 14 ElseIf Len(targetCell.Value) > 10 Then targetCell.WrapText = True targetCell.EntireRow.AutoFit Else: targetCell.RowHeight = 10.5 End If End If Next targetCell End Sub 

我不明白这一切。 你想Excel自动调整行高度文本量? 那么你的第三个“例外”应该是

 Else: targetCell.WarpText = true