Home » excel » Setting RowHeight Excel VBA

Setting RowHeight Excel VBA

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’ve been struggling for several hours to set row heights for an implied range. The code works except for two problems 1. ALL rows with data are set to AutoFit instead of just the intended range and 2. I cannot seem to add ‘3’ to the row height per the 2nd to last line of code:

Sub SetRH()

    ActiveSheet.Unprotect
    Application.ScreenUpdating = False

    Range("C" & (ActiveCell.row)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection.Offset(0, 0), Selection.Offset(0, 4)).Select

    Selection.sort Key1:=Range("C6"), Order1:=xlAscending, Key2:=Range("E6") _
       , Order2:=xlAscending, Key3:=Range("D6"), Order3:=xlAscending, Header _
        :=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

    For Each row In ActiveSheet.UsedRange.Rows: Rows.AutoFit: Next
    For Each row In ActiveSheet.UsedRange.Rows: Rows.RowHeight = Rows.RowHeight + 3: Next

    Application.ScreenUpdating = True

End Sub

Any help is much appreciated!

How to&Answers:

The below code will loop through each row auto fit and then increase the row height by +3.

Dim ws As Worksheet
Set ws = ActiveSheet

Dim Rng As Range
Dim cel As Range
Set Rng = Range(ActiveCell, Cells(Rows.Count, ActiveCell.Column).End(xlUp))

For Each cel In Rng
    cel.Rows.AutoFit
    cel.Rows.RowHeight = cel.Rows.RowHeight + 3
Next cel