Home » excel » vba – EXCEL – IF statement with RowHeight as condition not working

vba – EXCEL – IF statement with RowHeight as condition not working

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a long Excel database which I created running a VBA Macro I didn’t originally create (thus I don’t know exactly how it works). The issue is that for some reason, the VBA Macro shrinks the height of some rows, which makes the text contained inside not always 100% visible. I’ve already made some changes to the Excel file, so I don’t really want to search for the flaw in the VBA I used, so I wanted to write a short VBA code that would increase the height of the rows with shrinked height:

Sub Macro1()
Dim rowIndex As Integer
Dim lastRowIndex As Integer
lastRowIndex = 15000
For rowIndex = 7 To lastRowIndex:
If ActiveSheet.Rows(rowIndex).RowHeight = 9.6 Then
ActiveSheet.Rows(rowIndex).RowHeight = 10.8
End If
Next rowIndex
End Sub

For some reason this code isn’t executing the way I thought (it does nothing). I checked whether I used the right height value, but everything seemed fine.

Did I miss something?

How to&Answers:

Your code works fine on rows that are actually 9.6 tall. Try increasing any row that is less than 10.8 tall.

Sub Macro1()
    Dim rowIndex As Integer
    Dim lastRowIndex As Integer
    lastRowIndex = 15000
    For rowIndex = 7 To lastRowIndex
        If ActiveSheet.Rows(rowIndex).RowHeight < 10.8 Then
            ActiveSheet.Rows(rowIndex).RowHeight = 10.8
        End If
    Next rowIndex
End Sub

Answer:

It seems you cannot have a RoweHeight of 9.6

Sub TestItA()
    ActiveSheet.Rows(7).RowHeight = 9.7
    Debug.Print ActiveSheet.Rows(7).RowHeight
End Sub

According to this post you can only set the height in fixed “increments”, or to be more precise in “pixel”

Although help, and the Row Height Dialog, say to set the row height in
points, Excel actually sets the row height in pixels.

1 pixel = 72 / 96 points= 0.75 points.

That is why you can only change the row height in increments of 0.75
points.