excelvba

Excel VBA: range.autofit not "autofitting"


I have the following code in Excel:

Sub autosort()
Dim lastrow As Long
lastrow = Cells(rows.Count, 17).End(xlUp).Row
Range("A3:Q" & lastrow).Sort key1:=Range("Q3:Q" & lastrow), _
   order1:=xlDescending, Header:=xlNo
Call autoborder(Range("A3:Q" & lastrow))
Range("A2:Q" & lastrow).EntireRow.AutoFit
End Sub

Everything works to border up the cells, but I can't get the sheet to auto-fit the row heights via the last line...it runs but when I go back to look at the result the row heights are not autofit.

Any tips would be greatly appreciated, thank you.


Solution

  • You could try:

    Sub test()
    
        With ThisWorkbook.Worksheets("Sheet1")
    
            .Cells.EntireColumn.AutoFit
            .Cells.EntireRow.AutoFit
    
        End With
    
    End Sub