Autofit Row Height when sorting

New Contributor

When a worksheet contains rows with different heights, I would expect those heights to be automatically adjusted during a sort of the rows in that sheet.  Alas, this appears to not be the case (At least I haven't been able to make that happen).  Can I make that happen?

7 Replies


You can manually adjust the column width or row height or automatically resize columns and rows to fit the data.

Change the column width or row height in Excel


Additional Information:

Change the column width and row height


Hope I was able to help you with this info.



I know I don't know anything (Socrates)



Thanks for your help. I guess I'm just to lazy to want to do it manually.


Here automatic adjustment with VBA on all cells
See the attached file with examples where you can try it out yourself.

Enjoy excel

I experimented on an excel worksheet with rows of different heights and inserted a variety of objects (pictures, text boxes, etc,) and, sure enough, when I resorted the rows, autofit did not function. I also tried reformatting the objects . . . no luck! I'm satisfied. Thanks for looking at this with me.


I am pleased that you are satisfied, I hope to see you again in the future here in the forum.
Welcome to your Excel discussion space!


Also wish you have fun with Excel.



Hello Niko...

Your example file below is exactly what I'm trying to do.  But apparently I'm not be doing the VBA correctly, because it's not working for me either.  I even tried copying your code exactly, but it didn't do a thing.  (I've attached your example to clarify what I'm referring to.)   

Preview file
20 KB
I resorted to creating a Macro/Button, but I'd rather it be done just as you have in your example, "Autofit Automatically".  I was even able to use your example and I added Data/Filter/Sort, and even after sorting, it still works!  
If there are some specific steps to this VBA Code, I'd be most interested.




VBA code "behind" a worksheet or a workbook may not work in Excel

o edit code "behind" a worksheet or a workbook:

  • Activate the Visual Basic Editor (press ALT+F11).

  • In the Project Explorer window, you will see entries similar to the following:

Copy this Code in the Worksheet you like


Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
End Sub


 With this you don't need a button or any other jump starter, the sheet should behave just like the sheet I sent you. If you cannot manage it on your PC, I can only help if you send me the file (without sensitive data).


*Knowing the Excel version, as well as the operating system and storage medium (OneDrive, hard drive, Sharepoint, etc.) would be an advantage to share more detailed information. 


Thank you for your understanding and patience



I know I don't know anything (Socrates)