tec*_*tle 1 microsoft-excel-2010
我们有一个 Excel 工作表,上面有各种合并和未合并的单元格,所以它像纸质表格一样排列。底部是一些单元格,它们在整个页面上一直合并,因为它们可以包含大量数据。但是,如果有人写入的内容超出单元格宽度,它会环绕文本但不显示它(在这些单元格上启用了环绕)。即使您双击单元格之间的线(就像您自动调整高度一样),它也将保持在单行高度。我可以手动调整高度,但因为我们通常锁定工作表,用户不能这样做。
有没有办法让它在合并的单元格上自动调整?设置最好,但 VBA 也可以。我试过类似的代码,Cells.EntireRow.AutoFit但仍然只到一行高。
这是一篇 Contextures 博客文章,标题为“Autofit Merged Cell Row Height”
我敦促你阅读整篇文章,但这是代码:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim MergeWidth As Single
Dim cM As Range
Dim AutoFitRng As Range
Dim CWidth As Double
Dim NewRowHt As Double
Dim str01 As String
str01 = "OrderNote"
If Not Intersect(Target, Range(str01)) Is Nothing Then
Application.ScreenUpdating = False
On Error Resume Next
Set AutoFitRng = Range(Range(str01).MergeArea.Address)
With AutoFitRng
.MergeCells = False
CWidth = .Cells(1).ColumnWidth
MergeWidth = 0
For Each cM In AutoFitRng
cM.WrapText = True
MergeWidth = cM.ColumnWidth + MergeWidth
Next
'small adjustment to temporary width
MergeWidth = MergeWidth + AutoFitRng.Cells.Count * 0.66
.Cells(1).ColumnWidth = MergeWidth
.EntireRow.AutoFit
NewRowHt = .RowHeight
.Cells(1).ColumnWidth = CWidth
.MergeCells = True
.RowHeight = NewRowHt
End With
Application.ScreenUpdating = True
End If
End Sub
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17378 次 |
| 最近记录: |