2010-09-01 58 views
1

我正在慢慢修改和扩展If ... ElseIf ... Else语句(see post)来帮助我格式化一长串类别和子类别(感谢文件LLunatik)。Excel宏:如何扩展行高以适应包装文本?

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

我不能简单地刷新屏幕的声明说,任何线不是一个例外(粗体),或异常两(上标)应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 

能I:

  • 查找那些targetCells有超过60个字符(固定柱的宽度)
  • 应用.WrapText于那些特定targetCells
  • 自动扩展只有那些targetCells(因此不会覆盖我的标准10.5pt行,用于其他非异常targetCells)。

这项工作?它是否需要放置在单独的子例程中,因为第一个参数是?它看起来像什么? (看到我下面的尴尬努力)

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

回答

1

这似乎工作。

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 
+0

就像一个魅力。非常感谢。它为我节省了很多时间。 – RocketGoal 2010-09-03 11:59:15

0

我不明白这一切。你是否希望Excel自动调整行高以达到文本的数量?那么你的第三个'例外'应该是

Else: targetCell.WarpText = true 
+0

因为我已经分配了10.5的特定高度.WrapText似乎不工作(我试过了)。我需要.AutoUpdate来扩展线条,然后不幸的是10.5的特定线条会改变高度。解决办法是删除10.5,但包括粗体和上标例外。我需要询问boxx是否可以删除10.5并使用自动大小。 – RocketGoal 2010-09-02 13:44:52