Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make the worksheet columns automatically change size to fit the longest entry

Presentation

Make the worksheet columns automatically change size to fit the longest entry

by  JESTAR  Posted    (Edited  )
When data is entered into a cell on your worksheet, should that cell not be wide enough to fit the data, it will cut off. You then have to resize the column manually. This can be excessive if your forms/macros are entering data into worksheets and you have to go through each one every time autofitting them.

In the VBA editor, open up the code window for any of the Worksheets, and paste this code:

[tt]Option Explicit

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Columns(Target.Column).AutoFit
End Sub
[/tt]


And that's it. Any data entered into that worksheet, be it manually or through a form/macro, will cause the columns to automatically expand/shrink to fit the longest entry.

N.B. Paste it into multiple Worksheet code windows, or into ThisWorkbook code window.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top