动态增加合并细胞的高度

时间:2018-06-28 00:16:56

标签: vba macos excel-vba excel

我想在长度会变化的Excel单元格中显示文本。单元格应容纳超过5K个字符。这就是为什么我合并了三个相邻的单元格和三行。但是,根据输入文本的长度,单元格的高度不会动态变化。

我已实现的示例代码:

Sub pqrs(ByVal target as Range)
   Dim r as Range
   Set a = Worksheets("Sheet1").Range(B1:D3) 
   a.Value = "large text........."
   abcd.a '--- Called below Proc.
   a.Merge
End Sub

Sub abcd(ByVal target as Range)
   Dim r as Range
   Dim defaultHeight as Integer
   Dim maxtHeight as Integer
   Dim length as Integer
   Dim heightToUse as Double

   defaultHeight = 12
   maxHeight = 409
   taget.merge

   For Each r in target.Cells(1)
        length = Len(r.Value)
        If length >= 1000 and length <= 2000 Then
           heightToUse = defaultHeight + 100
        If (heightToUse > maxHeight) Then
            r.RowHeight = maxHeight
        Else
             r.RowHeight = heightToUse
        End If  
        ElseIf length > 2000 and length <= 4000 Then
            heightToUse = defaultHeight + 200
        If (heightToUse > maxHeight) Then
            r.RowHeight = maxHeight
        Else
            r.RowHeight = heightToUse
        End If  
    Else
          r.RowHeight = defaultHeight
    End If
          r.WrapText = True
  Next r
 End Sub

1 个答案:

答案 0 :(得分:0)

我在您的代码中看到了多个问题。

1)@Jeeped指出,不仅“合并”被拼写为“ mergge”,“目标”也被拼写为“ taget”

2)Dim maxtHeight As Integer应该是Dim maxHeight As Integer

3)For Each r In target必须是For Each r in target.rowsFor Each r in target.cells,或者类似的东西,在没有看到您的示例的情况下我无法真正分辨出来。

4)您可能需要合并文本才能获得我认为想要的结果(例如r.WrapText = True)。

当我纠正了所有错误后,高度确实发生了变化,但是我不确定它是否确实达到了您想要的结果,因为我没有看到您的电子表格或实际数据。请更正所有这些错别字。如果代码仍然无法使用,请发布您的电子表格示例,以便我们查看实际的布局和数据。如果有效,请记住标记为已回答,以便人们知道您的问题已解决。

相关问题