Autofit columns

Rainmanne

Board Regular
Joined
Mar 10, 2016
Messages
132
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

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
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,223,897
Messages
6,175,270
Members
452,628
Latest member
dd2

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