[英]Fix code request for 'Auto Fit Row Height Of Merged Cells' formula. VBA Excel
The primary issue with this macro is that when the text is too long, the height of the merged cells becomes too large. 此宏的主要问题是,当文本过长时,合并单元格的高度将变得太大。
The thread on from the source(listed below), does not have any really satisfying solutions to the issue. 从源头开始的线程(在下面列出)没有任何真正令人满意的解决方案。
The merged cell takes info from several sources and includes 'char(10) spaces that make it difficult to create a single cell column for auto-fitting. 合并的单元格从多个来源获取信息,并包含'char(10)空间,这使得难以创建单个单元格列进行自动拟合。
Option Explicit
Public Sub AutoFitAll()
Call AutoFitMergedCells(Range("a1:b2"))
Call AutoFitMergedCells(Range("c4:d6"))
Call AutoFitMergedCells(Range("e1:e3"))
End Sub
Public Sub AutoFitMergedCells(oRange As Range)
Dim tHeight As Integer
Dim iPtr As Integer
Dim oldWidth As Single
Dim oldZZWidth As Single
Dim newWidth As Single
Dim newHeight As Single
With Sheets("Sheet4")
oldWidth = 0
For iPtr = 1 To oRange.Columns.Count
oldWidth = oldWidth + .Cells(1, oRange.Column + iPtr - 1).ColumnWidth
Next iPtr
oldWidth = .Cells(1, oRange.Column).ColumnWidth + .Cells(1, oRange.Column + 1).ColumnWidth
oRange.MergeCells = False
newWidth = Len(.Cells(oRange.Row, oRange.Column).Value)
oldZZWidth = .Range("ZZ1").ColumnWidth
.Range("ZZ1") = Left(.Cells(oRange.Row, oRange.Column).Value, newWidth)
.Range("ZZ1").WrapText = True
.Columns("ZZ").ColumnWidth = oldWidth
.Rows("1").EntireRow.AutoFit
newHeight = .Rows("1").RowHeight / oRange.Rows.Count
.Rows(CStr(oRange.Row) & ":" & CStr(oRange.Row + oRange.Rows.Count - 1)).RowHeight = newHeight
oRange.MergeCells = True
oRange.WrapText = True
.Range("ZZ1").ClearContents
.Range("ZZ1").ColumnWidth = oldZZWidth
End With
End Sub
Source: https://www.extendoffice.com/documents/excel/2342-excel-autofit-row-height-merged-cells.html?page_comment=2 资料来源: https : //www.extendoffice.com/documents/excel/2342-excel-autofit-row-height-merged-cells.html?page_comment=2
Try adding the line below: 尝试添加以下行:
oRange.Rows(oRange.Rows.Count).EntireRow.AutoFit
After: 后:
oRange.MergeCells = True
oRange.WrapText = True
In your code above 在上面的代码中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.