简体   繁体   中英

Excel VBA calculate height of row

I have several rows in a workbook that I am working on that require both merged columns, and merged rows. For this reason, Excel's auto fit does me no good. It is also possible, and probable, that at some point these will need to expand beyond Excel's 409.5pt limit for row height.

For this reason, I am looking for a way to calculate the total necessary height of a set of rows so they will fit a block of text properly using VBA.

How can I calculate the necessary row height to fit a block of text, knowing that it will exceed 409.5pt? The font size and cell width will be constant, so that can be used to calculate it necessary.

12pt font is like 1/6", ie, there are 72 points in an inch. using your font size, you should be able to come up with an algorithm that reasonably approximates the necessary row height.

Use something like this to print a list of font sizes & row heights, and use the results to come up with your conversion formula.

Sub Test()

Dim r As Range
Dim i as Integer

Set r = [a1]
r.EntireRow.AutoFit = True
For i = 1 to 10

    MsgBox "Row height = " & r.Height & vbCrLF & _
            "Font size = " & r.Font.Size

    Debug.Print r.Height & vbTab & r.Font.Size

    r.Font.Size = r.Font.Size + 1
Next

End Sub

Width is trickier since individual characters have different widths ( i is much narrower than W in many fonts), although there are some functions out there which will calculate the width of each individual character for common fonts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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