Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

VBA Visual Basic for Applications (Microsoft) FAQ


Make the worksheet columns automatically change size to fit the longest entry by JESTAR
Posted: 7 Aug 03

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:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
End Sub

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.

Back to VBA Visual Basic for Applications (Microsoft) FAQ Index
Back to VBA Visual Basic for Applications (Microsoft) Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close