Search code examples
vbaheight

vba excel AutoFit cells height is not working


I want that the height of the text in a current cell to be automatically adjusted. HEIGHT not width.

I tried this code and it is not working so far.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    With Worksheets("Lohnbeurteilung").Cells
        .EntireRow.AutoFit
        .WrapText = True
    End With
    
End Sub

Solution

  • Try (tested):

    With Worksheets("Lohnbeurteilung").UsedRange.EntireRow
        .WrapText = True
        .AutoFit
    End With
    

    Note. If you need to autofit only the selected range, you can use With Target.EntireRow in Worksheet_SelectionChange