简体   繁体   English

Excel VBA自动拟合合并单元格

[英]Excel VBA Autofit Merged Cells

Dear Stackoverflow users, 尊敬的Stackoverflow用户,

For a project i would like to adjust the height of a merged row to fit the contents. 对于项目,我想调整合并行的高度以适合内容。

I found the following code on "extendoffice.com". 我在“ extendoffice.com”上找到了以下代码。 ( https://www.extendoffice.com/documents/excel/2342-excel-autofit-row-height-merged-cells.html?page_comment=3 ) https://www.extendoffice.com/documents/excel/2342-excel-autofit-row-height-merged-cells.html?page_comment=3

The code looks clean and good, but i can't get it to work correctly, i think it's due to the different sizes of the columns. 该代码看起来很干净而且很好,但是我无法使其正常工作,我认为这是由于列的大小不同所致。 The height is just always way to large. 高度总是很大。

I already tried to get a constant to divide the outcome by 2 or another factor, but this is not working. 我已经尝试获取一个常数,将结果除以2或其他因子,但这不起作用。

Could you a look and give me guidance how to solve the issue i'm encountering that the height is way larger than necessary. 您能否看一下并给我指导,如何解决我遇到的高度远远超出必要高度的问题。

The Example File: Example File 示例文件: 示例文件

The Code: 编码:

    Option Explicit

Public Sub AutoFitAll()

  Call AutoFitMergedCells(Range("B4:K4"))
  Call AutoFitMergedCells(Range("B5:K5"))
  Call AutoFitMergedCells(Range("B6:K6"))

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("Lead")
    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

Thanks in advance! 提前致谢!

Regards, Dubblej 问候,Dubblej

So I gave it a try as per Allen Wyatt's suggestion here . 所以我给它一个尝试按照艾伦悦的建议在这里

He suggested using a helper column, in my case, column P (shouldn't be immediately adjacent) and have a cell with identical formatting (except for merging) pointing at the top left cell of your merged range. 他建议使用一个辅助列,在我的情况下,该列为P列(不应紧邻),并具有一个格式相同(合并除外)的单元格指向合并范围的左上方单元格。

So if you had the following in merged range B4:K4 : 因此,如果您在合并范围B4:K4具有以下内容:

合并范围内的文字

Which originally was compressed like so: 最初是这样压缩的:

压缩文字检视

In P4 put the formula =B4 P4放公式=B4

Then in a standard module put 然后放在标准模块中

Option Explicit

Sub Autofit()

    ActiveSheet.Range("P4").Rows.Autofit

End Sub

Seems to work. 似乎可以工作。

This problem seems easy, but you can see that there are several exceptions to use. 这个问题似乎很容易,但是您可以看到有几个例外可以使用。 Actually necessary code is more than 10 times the size of simple code. 实际上,所需的代码是简单代码大小的10倍以上。

I made add-in for Auto fit row height of multiple merged cells. 我为多个合并的单元格的“自动适合行高”添加了插件。 Please use this, if you want to autofit row hight. 如果要自动调整行高,请使用此选项。 [Release Ver2.6 · toowaki/AutoFitRowEx · GitHub] https://github.com/toowaki/AutoFitRowEx/releases/tag/2.6.2 [发布Ver2.6·toowaki / AutoFitRowEx·GitHub] https://github.com/toowaki/AutoFitRowEx/releases/tag/2.6.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM