残念ながら、良い解決策は見つかりませんでした。この問題は Excel 2000 のバグが原因です。それ以降のバージョンにも当てはまるかどうかはわかりません。
この問題は、セルを水平方向にマージするときに現れます。セルを結合すると、行の高さが自動調整されません。
次のコード例は、問題を示しています
Dim r As Range
Set r = Sheet1.Range("B2")
Range(r, r(1, 2)).Merge
r.Value = ""
r.Value = "asdg lakj dsgl dfgjdfgj dgj dfgj dfgjdgjdfgjdfgjd"
r.WrapText = True
r.EntireRow.AutoFit
この場合、r.EntireRow.AutoFit は、テキストが複数の行にまたがることを考慮せず、1 行のテキストであるかのように高さを調整します。
セルとワードラップが結合された行に手動で自動調整 (シートの行高さ調整をダブルクリック) を行うと、同じ問題が発生します。
解決策 (Gary McGill が提案) は、無関係なシートを使用することです。結合されたセル全体に一致するように列幅を設定します。同じ書式でテキストをコピーします。セルを自動調整して、そのセルの値を使用します。
簡単な例を次に示します。
Public Sub test()
Dim widthInPoints As Double
Dim mergedCells As Range
Set mergedCells = Sheet1.Range("B2:C2")
widthInPoints = mergedCells.width
Dim testCell As Range
Set testCell = Sheet2.Range("A1")
testCell.EntireColumn.columnWidth = ConvertPointsToColumnWidth(widthInPoints, Sheet2.Range("A1"))
testCell.WrapText = True
testCell.Value = mergedCells.Value
'Text formating should be applied as well, if any'
testCell.EntireRow.AutoFit
mergedCells.EntireRow.rowHeight = testCell.rowHeight
End Sub
Private Function ConvertPointsToColumnWidth(widthInPoints As Double, standardCell As Range) As Variant
ConvertPointsToColumnWidth = (widthInPoints / standardCell.width) * standardCell.columnWidth
End Function