LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #4   Report Post  
Posted to microsoft.public.excel.programming
Tim Tim is offline
external usenet poster
 
Posts: 408
Default Automatically resize a cell

Can you make this apply to only 1 or 2 columns in a worksheet?
Thanks

"Rick Rothstein (MVP - VB)" wrote:

You can put this code in the code window for the sheet you want this
functionality on...

*************** START OF CODE ***************
Dim ColWidth As Double

Private Sub Worksheet_Change(ByVal Target As Range)
With Target
Application.ScreenUpdating = False
.EntireColumn.AutoFit
If .ColumnWidth <= ColWidth Then
.ColumnWidth = ColWidth
End If
Application.ScreenUpdating = True
End With
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
ColWidth = Target.ColumnWidth
End Sub
*************** END OF CODE ***************

This will leave the column's width alone if the text you enter into the cell
is smaller than, or equal to, the current width of the column; however, if
you enter text that is longer than the width of the column, the column will
resize to fit the text. However, note that the opposite is not true... if
you change the text in the cell containing the widest text string to a text
string that is shorter than its current width, the column will NOT shrink
down to the new maximum width. The reason I did it this way is because
simply auto-fitting the column would shrink it down to the size of the
smallest text string which could be too narrow for your purposes. If that is
not acceptable, then let me know exactly how you want it to function and I
will change the code accordingly.

Rick


"straightedge32" wrote in message
...
I was wondering if there was a way to (via macros) enter text into a cell,
and then resize that cell to accomodate the text? Sometimes I enter text
into cells and the text is larger than the default size of a cell, and I
would like to have code that adjusts the size of the column to accomodate
the
entire text (so that I don't have to do it manually).

Is there a fast way to do that? Sample code would be greatly appreciated.
Thanks in advance.

-Mike



 
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
Automatically resize a picture to a cell Gpompidou Excel Programming 2 April 23rd 08 12:54 AM
Pie Charts Resize Automatically - How To Avoid? John Mansfield Charts and Charting in Excel 0 May 11th 05 08:12 PM
Urgent help!! How to automatically resize all the charts? Thank you! crossingmind Excel Programming 2 April 28th 05 04:58 PM
Urgent help! how to automatically resize all the charts? crossingmind Excel Worksheet Functions 2 April 28th 05 04:56 PM
Can Columns Resize Automatically??? Todd Huttenstine\(Remote\) Excel Programming 3 November 30th 03 09:41 AM


All times are GMT +1. The time now is 05:04 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"