Autofit columns

Rainmanne

Board Regular
Joined
Mar 10, 2016
Messages
120
Office Version
  1. 2013
Platform
  1. Windows
I need to auto fit the visible columns with the data after the data are imported. I tried to use this:

VBA Code:
ActiveSheet.Cells.SpecialCells(xlCellTypeVisible).Columns.AutoFit

and this
VBA Code:
ActiveSheet.Cells.SpecialCells(xlCellTypeVisible).EntireColumn.AutoFit

But what the code does is makes all the visible columns with data standard size rather auto fit them to the widest cell. I wonder what I am doing wrong?
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
This works just fine for me:
ActiveSheet.Cells.SpecialCells(xlCellTypeVisible).Columns.AutoFit

Are you sure that you don't have the "Wrap text" format set for these columns?
That would prevent it from working the way you want. You want that option to be unchecked.

1690453881954.png


You could ensure that in your VBA code like this:
VBA Code:
ActiveSheet.Cells.SpecialCells(xlCellTypeVisible).Columns.WrapText = False
ActiveSheet.Cells.SpecialCells(xlCellTypeVisible).Columns.AutoFit
 
Upvote 0
This works just fine for me:


Are you sure that you don't have the "Wrap text" format set for these columns?
That would prevent it from working the way you want. You want that option to be unchecked.

View attachment 96047

You could ensure that in your VBA code like this:
VBA Code:
ActiveSheet.Cells.SpecialCells(xlCellTypeVisible).Columns.WrapText = False
ActiveSheet.Cells.SpecialCells(xlCellTypeVisible).Columns.AutoFit
Thanks a lot for the tip about the WrapText! It was not that but I will now use it just in case.
The problem as I realised was that I use code optimisation:
VBA Code:
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
ActiveSheet.DisplayPageBreaks = False
Application.DisplayAlerts = False

And it prevented AutoFit working properly. I have now moved AutoFit after stopping the optimisation:
VBA Code:
ActiveSheet.DisplayPageBreaks = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True

and it works properly now.
 
Upvote 0
Or maybe you just need to turn ScreenUpdating on back at the end of your code! ;)
VBA Code:
Application.ScreenUpdating = True
 
Upvote 0
Or maybe you just need to turn ScreenUpdating on back at the end of your code! ;)
VBA Code:
Application.ScreenUpdating = True
😂

No, no, it's turned on but at the very end of the code 👍
 
Upvote 0
😂

No, no, it's turned on but at the very end of the code 👍
That is where is should be.

Just make sure that your don't have anything that would exit the code before that point (and never run those lines).

I really don't think any of those other lines of code you posted should interfere with AutoFit.
You may have other things at play, but since you have not posted the entire code block, I can only guess.
But if moving the code around gets it work, then I guess you can go with that.
 
Upvote 0

Forum statistics

Threads
1,215,143
Messages
6,123,277
Members
449,093
Latest member
Vincent Khandagale

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top