简体   繁体   中英

Why is Excel VBA Row Height not equal to Range Height

The following two methods are returning different results and I'm trying to understand what the relationship is.

Method 1: I manually set the row height by right clicking on the row. I sum up the height of a few of these rows to get a total height of a range of rows.

Method 2: I use the Range.Height property in VBA to get the total height of that set of rows.

If I simplify my range to just a single row, this will be different in cases where the manual row height is not a multiple of 3, but the relationship does not appear to be linear.

Code to get range height:

Sub range_height()
    MsgBox Range("A1").Height
End Sub

Set row 1 height = 15 and range_height returns a message box of 15.

Set row 1 height = 20 and range_height returns a message box of 19.5.

My ultimate goal is to be able to add padding to a range so that when I copy into PowerPoint, I have identical dimensions and my slides are pleasant to look at. I can do this via trial and error, but I feel like there must be a better way.

I had faced a similar problem while trying to draw lines in excel using VBA. I was using range.top and range.height for deriving the co-ordinates for my work. However, lines were getting drawn at slightly offset location than expected. Further I found that offset was varying with varying row sizes… Finally I figured out that for row height =15, row height and range height were matching. So, I changed all row height to 15, created my lines using VBA and then changed the row height as per requirement. Lines location was now matching my requirement!

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