Excel宏:如何扩展行高以容纳换行文字?
我正在慢慢地修改和扩展If…ElseIf…Else语句(),以帮助我格式化一个长长的类别和子类别列表(感谢marg和Lunatik) 我已将固定行高指定给90%的范围/行。现在,我被困在那些单元格中,单元格中有大量的文本,这些文本在一个单元格中包含两行。两行文字不符合我的10.5标准高度 我不能简单地刷新屏幕,因为声明中说,任何不是异常1(粗体)或异常2(上标)的行都应该是10.5pts。我需要第三个例外。 我目前有:Excel宏:如何扩展行高以容纳换行文字?,excel,vba,if-statement,Excel,Vba,If Statement,我正在慢慢地修改和扩展If…ElseIf…Else语句(),以帮助我格式化一个长长的类别和子类别列表(感谢marg和Lunatik) 我已将固定行高指定给90%的范围/行。现在,我被困在那些单元格中,单元格中有大量的文本,这些文本在一个单元格中包含两行。两行文字不符合我的10.5标准高度 我不能简单地刷新屏幕,因为声明中说,任何不是异常1(粗体)或异常2(上标)的行都应该是10.5pts。我需要第三个例外。 我目前有: Sub setHeights() Dim targetRange As Ra
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
我可以:
- 查找超过60个字符(固定列的宽度)的目标单元格
- 将.WrapText应用于那些特定的TargetCell
- 仅自动展开这些目标单元格(因此不会覆盖其他非异常目标单元格的标准10.5pt行)
我不完全明白。是否希望Excel根据文本量自动调整行高?那么你的第三个“例外”应该是
Else: targetCell.WarpText = true
我不完全明白。是否希望Excel根据文本量自动调整行高?那么你的第三个“例外”应该是
Else: targetCell.WarpText = true
这似乎奏效了
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
这似乎奏效了
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
因为我指定了一个10.5的特定高度。WrapText似乎不起作用(我已经尝试过了)。我需要。自动更新以使线条展开,然后10.5的特定线条不幸更改高度。解决方案是删除10.5,但包括粗体和上标例外。我需要询问boxx是否可以删除10.5并使用自动大小。因为我指定了10.5的特定高度。WrapText似乎不起作用(我尝试过)。我需要。自动更新以使线条展开,然后10.5的特定线条不幸更改高度。解决方案是删除10.5,但包括粗体和上标例外。我需要问boxx是否可以删除10.5并使用自动大小。就像一个符咒。非常感谢。它为我节省了很多时间,就像一个符咒。非常感谢。这节省了我很多时间。