Reputation: 887
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.
Upvotes: 0
Views: 3731
Reputation: 53663
12pt font is like 1/6", i.e., 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.
Upvotes: -1