I need to measure row heights to ensure that a printed sheet will fit on a certain number of pages.
When I read row heights from Excel using VBA, they differ from the row height I see in Excel when I right-click a row to view height.
For example, I have three rows at the top of a sheet. In VBA, I set the height of each row:
Public Sub setTitleAreaCellsToDefaultHeight()
vCompanyNameCell.EntireRow.RowHeight = cHTitleCompanyLine '30
vReportNameCell.EntireRow.RowHeight = cHTitleReportLine '21
vReportDateCell.EntireRow.RowHeight = cHTitleDateLine '31
Stop
End Sub
When the code breaks on Stop
above, I go to the sheet and view the height of the first row, and it is correct at 30.
After these heights are set, I have VBA code that reads the height of each cell:
Public Sub getCurrentHeights()
vHCurrCompanyCell = vCompanyNameCell.EntireRow.height
vHCurrReportCell = vReportNameCell.EntireRow.height
vHCurrCompanyCell = vReportDateCell.EntireRow.height
Stop
End Sub
When I Stop
in the code above, then hover over the values in the first line in the VBA editor, I see this:
VBA is saying the height is 30, which is what I set it to, but somehow the result is off by 0.75. So is the height of the row 30 or 30.75?
Also how is the left side of the equation not the same as the right side?
I thought it had to do with .RowHeight.Height
property being a double, but if I round the right side of the equation to 0 decimals, I still get crazy results:
Even though I rounded the double to 0 decimals, it's still saying the height of the cell is 30.75 instead of 30.
Similar problem here https://www.mrexcel.com/board/threads/vba-formula-for-row-height-gives-incorrect-amount.235649/, but their solution was to hard code the heights and I can't do that, I need to read heights as they are.
So 2 questions:
Here is a reproducible example.
On my computer, the value of outputHeight3
turns out to be 30.75 instead of 31.
Is the height on the worksheet 30.75 or 31?
Public Sub test()
Stop
Dim companyNameHeight As Integer
Dim reportNameHeight As Integer
Dim reportDateHeight As Integer
Dim height As Double
Dim rounded As Double
Dim outputHeight1 As Double
Dim outputHeight2 As Double
Dim outputHeight3 As Double
Dim companyNameCell As Range
Dim reportNameCell As Range
Dim reportDateCell As Range
companyNameHeight = 30
reportNameHeight = 21
reportDateHeight = 31
'set height of the rows
Set companyNameCell = Worksheets("OUTPUT - BS").Range("OUTPUT_BS_COMPANY_NAME")
companyNameCell.EntireRow.RowHeight = companyNameHeight
Set reportNameCell = Worksheets("OUTPUT - BS").Range("OUTPUT_BS_REPORT_NAME")
reportNameCell.EntireRow.RowHeight = reportNameHeight
Set reportDateCell = Worksheets("OUTPUT - BS").Range("OUTPUT_BS_REPORT_DATE")
reportDateCell.EntireRow.RowHeight = reportDateHeight
Stop
'read height of the rows
outputHeight1 = vCompanyNameCell.EntireRow.height
outputHeight2 = reportNameCell.EntireRow.height
outputHeight3 = reportDateCell.EntireRow.height
Stop
End Sub
You need to distinguish between RowHeight
and Height
of a Range.
You can set the RowHeight
to any number you want (except there is a maximum value of 409), but Excel will round it to the nearest quarter.
The Height
is a read-only value and will be calculated depending on the RowHeight
, but the result must be an integer value of Pixels: Change the Row height in Excel using the mouse: You will see that you can increase/decrease the height only by steps of 1 Pixel.
The measurement of RowHeight (and Height) is in Points, and there are usually 72 Point per Inch. The number of Pixels per Inch is defined by the device (your monitor). The most common value is 96 Pixel per Inch, therefore the formula would be
pixels = height * 96 / 72
height = pixels * 72 / 96
That means 14 pixels would result in 14 * 72 / 96 = 10.5
The next possible bigger height would be 15 pixels = 15 * 72 / 96 = 11.25
There can't be a Height
value in between 10.5 and 11.25
If you are interested:
Use Application.InchesToPoints
to get the number of of points per Inch
Use Workbook.WebOptions.PixelsPerInch
to get the number of pixels per Inch.