Forum Discussion
Autofit Row Height when sorting
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.)
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 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)
- 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?- Kingram01Dec 21, 2022Copper ContributorI figured it out and it worked like a wonder! This is awesome!!!
- NikolinoDEDec 21, 2022Gold ContributorSo it works for me (Excel 2016). Did you save the file as .Xlsm?
- Kingram01Dec 21, 2022Copper Contributor
NikolinoDE I was able to get it to work on one of my workbooks and actually was able to remove column autofit and it still worked after sorting multiple different ways. Now my issue is.... I cant get it to do the same thing on another workbook by following the same exact example. It is odd. I have saved my add in and tried that as well with no luck. What am I doing wrong?
- 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.