Home > Net >  VBA/Excel, Why IF condition has no effect with Row Height in this code?
VBA/Excel, Why IF condition has no effect with Row Height in this code?

Time:10-30

I am using below code to set Row Height of Range to not less than 45. This line of code works Rng.EntireRow.RowHeight = 45 But If I used it with IF condition , It has no effect at all. I tried to replace EntireRow with ROWS , but also no effect.

Sub Row_Height()

    Dim Rng As Range
    
    Set Rng = ActiveSheet.Range("A3", Cells(Rows.count, "A").End(xlUp))
    
    If Rng.EntireRow.RowHeight < 45 Then
       Rng.EntireRow.RowHeight = 45
    End If

End Sub

CodePudding user response:

Rng is not a single cell, so the rows in it could all have different heights.

Loop thru the rows in it instead so you can check the height one by one.

Sub Row_Height()
    Dim r As Range
    Dim Rng As Range
    Set Rng = ActiveSheet.Range("A3", Cells(Rows.Count, "A").End(xlUp))
    For Each r In Rng.Rows
        If r.RowHeight < 45 Then r.RowHeight = 45
    Next
End Sub
  • Related