Forum Discussion
Autofit Row Height when sorting
Here automatic adjustment with VBA on all cells
See the attached file with examples where you can try it out yourself.
Enjoy 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.)
- NikolinoDEFeb 06, 2022Platinum Contributor
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) Target.EntireRow.AutoFit Target.EntireColumn.AutoFit End SubWith 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)
- PlotinusReduxJan 04, 2023Copper Contributor
Just for anyone else that runs into this, Worksheet_Change() did not work for me, but putting the code under Worksheet_SelectionChange() did even though I'm not actually changing the selection--it works even if I've got a random cell above the table selected. <shrug>
Thanks for the tip, just needed to find the event that worked for me even though that event doesn't make any sense. I just needed the row part, not the columns. - Kingram01Dec 21, 2022Copper ContributorNikolinoDE,
When looking at your formula above in black... I highlighted my entire sheet and typed just as yours above, which did not work. Should I replace some words with my excel worksheet/tab name?- NikolinoDEDec 21, 2022Platinum ContributorSo it works for me (Excel 2016). Did you save the file as .Xlsm?