Autofit.rowheight取决于该行每个单元格中的文本(字符串)

时间:2018-09-13 12:34:54

标签: excel vba excel-vba

记住:

  1. 不同的column.width,
  2. 每个单元格具有不同的字符串(长度)
  3. 必须显示所有文本!

这不容易,因为您不知道每行有多少空间? 不剪切最大的单元格(例如,当我手动执行此操作时,有时最后一行被剪切掉) (就像自动调整一样,因为自动调整仅查看一个单元格(.select)而不是该行中的所有单元格,如果存在另一个具有更多内容的单元格,它将被切断)

如果有人对我有提示,那就太棒了!

到目前为止,这是我的代码:

Sub dynamicRowFit() 'start of the sub
    Dim ws As Worksheet
    Dim aRng As Range 'maybe we just need one range?
    Dim bRng As Range
    Dim defRow As Integer
    Dim defColmn As Integer

    Set defRow = 'enter the row to start(as Integer)
    Set defColmn = 'enter the Column to start (as Integer)
    Application.ScreenUpdating = False 'helps to prevent screenfreezes
    Application.Calculation = xlCalculationManual 'defines window
    Set ws = Worksheets("Sheet1")
    Set aRng = ws.Range(ws.Cells(defRow, defCulmn), ws.Cells(ws.Rows.Count, 1).End(xlUp))

'this is a defined loop with defined height, I need a dynamic solution:

    For Each bRng In aRng
        If Len(bRng) <= 90 Then 'when lengh of string is smallerOrequal to 90
            bRng.EntireRow.RowHeight = 15
        Else
            bRng.EntireRow.AutoFit
        End If
    Next bRng

    Application.Calculation = xlCalculationAutomatic 'till all cells emty
    Application.ScreenUpdating = True'Enables the update of screen
End Sub 'end of sub

因此,它还取决于.size(类型大小(我只有一个11)和字体(每种字体的大小可以不同,意味着每个字符需要的空间)。 我尽力定义了我的问题,标签等;;-)

0 个答案:

没有答案