动态增加合并单元格的高度



我想在长度会变化的单元格中显示文本。单元格应容纳 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(正如@Jeeped指出的那样,"合并"不仅拼写为"mergegge","target"还拼写为"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(。

当我纠正您的所有错误时,高度确实会发生变化,但我无法确定它是否真正达到了您想要的结果,因为我还没有看到您的电子表格或实际数据。请更正所有这些拼写错误。如果代码仍然不起作用,请发布电子表格的示例,以便我们查看实际布局和数据。

最新更新