Excel column width

S

stan

How can I make columns auto adjust the width to fit the data as I put it in
without having to go to auto fit width after I enter the data?
 
G

Gary''s Student

A good question. Put this macro in the worksheet code area:

Private Sub Worksheet_Change(ByVal Target As Range)
Set t = Target
Set clA = Range("A:A")
If Intersect(t, clA) Is Nothing Then Exit Sub
clA.Columns.AutoFit
End Sub

It is coded for column A. After any change in a column A cell, the column
width is automatically adjusted.
 
R

Rick Rothstein \(MVP - VB\)

Here is a simpler event code procedure which will do the same thing...

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 Then Target.Columns.AutoFit
End Sub

Rick
 
R

Rick Rothstein \(MVP - VB\)

Go to the worksheet where you want to have this functionality, right click
the tab at the bottom of the worksheet and select View Code from the popup
menu that appears, then copy/paste the code Gary''s Student posted into the
code window that appears. If Column A is not the correct column (you didn't
tell us what column you were interested in, so Gary''s Student guessed at
Column A), then change the "A:A" argument in the Range("A:A") function call
to the column letters you actually want. That is it... when you type an
entry into Column A (or whatever column you change it to), the column will
automatically "AutoFit" when an entry is made into it. By the way, you can
use this simpler code I posted back to Gary''s Student in place of the code
he offered you.

Rick
 
S

stan

Thank you!
--
stan


Rick Rothstein (MVP - VB) said:
Go to the worksheet where you want to have this functionality, right click
the tab at the bottom of the worksheet and select View Code from the popup
menu that appears, then copy/paste the code Gary''s Student posted into the
code window that appears. If Column A is not the correct column (you didn't
tell us what column you were interested in, so Gary''s Student guessed at
Column A), then change the "A:A" argument in the Range("A:A") function call
to the column letters you actually want. That is it... when you type an
entry into Column A (or whatever column you change it to), the column will
automatically "AutoFit" when an entry is made into it. By the way, you can
use this simpler code I posted back to Gary''s Student in place of the code
he offered you.

Rick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top