#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Autofit -

Hi

I have been trying to write a piece of code to expand cells horizontally to
accommodate text to save manually expanding cells.

The auto fit function doesnt work when trying to format the whole sheet.

Can anyone suggets what I need to write?
--
NC
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Autofit -

autofits all columns, then sets any column greater than 55 to 55:

With wks
.Cells.Columns.AutoFit
For Each objColumn In .UsedRange.Columns
If objColumn.ColumnWidth 55 Then
objColumn.ColumnWidth = 55
End If
Next objColumn
End With


"Nicola" wrote:

Hi

I have been trying to write a piece of code to expand cells horizontally to
accommodate text to save manually expanding cells.

The auto fit function doesnt work when trying to format the whole sheet.

Can anyone suggets what I need to write?
--
NC

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Autofit -

This will probably do what you want (just change the worksheet reference in
the For Each statement to your own worksheet's name); what it does is expand
only those columns where the entry does not fit and leaves all other columns
alone)...

Sub AutoFitWhenNecessary()
Dim R As Range
Dim LastColumn As Long
Dim CurrentWidth As Double
On Error GoTo Whoops
Application.ScreenUpdating = False
For Each R In Worksheets("Sheet1").Columns
CurrentWidth = R.ColumnWidth
R.AutoFit
If R.ColumnWidth < CurrentWidth Then
R.ColumnWidth = CurrentWidth
End If
Next
Whoops:
Application.ScreenUpdating = True
End Sub

--
Rick (MVP - Excel)


"Nicola" wrote in message
...
Hi

I have been trying to write a piece of code to expand cells horizontally
to
accommodate text to save manually expanding cells.

The auto fit function doesnt work when trying to format the whole sheet.

Can anyone suggets what I need to write?
--
NC


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Autofit -

Hi Nicola,
Autofit will not work if the cells are WrapText = True
First convert WrapText to false and then try again.
Alok

"Nicola" wrote:

Hi

I have been trying to write a piece of code to expand cells horizontally to
accommodate text to save manually expanding cells.

The auto fit function doesnt work when trying to format the whole sheet.

Can anyone suggets what I need to write?
--
NC

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Autofit (Columns.EntireColumn.AutoFit) does not work Michiel via OfficeKB.com Excel Discussion (Misc queries) 3 February 10th 09 05:29 PM
autofit Elena Excel Programming 3 May 22nd 06 05:48 PM
autofit does not autofit rreneerob Excel Discussion (Misc queries) 1 October 17th 05 05:55 PM
Autofit ? Stuart[_21_] Excel Programming 0 April 26th 05 08:56 PM
Autofit kevin Excel Programming 3 October 4th 04 09:26 PM


All times are GMT +1. The time now is 10:26 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"