Autosize cells as data is entered

G

Guest

Hello.

I have my cells set to Wrap & the row is set to Auto-Fit. The "Number" is
set to General.

When I add text, it wraps, however, the cell-size is not changing! What do
I have to do to get the cell (or row height) to grow as text is added?
 
G

Gord Dibben

I'm betting you have some "merged cells" which won't Autofit without VBA code.

If this is the case and you're interested in the code, post back.


Gord Dibben MS Excel MVP
 
G

Guest

Nevermind! Just realized my cells are merged - therefore, none of the
solutions I was trying will work.
 
G

Guest

It is the case! I think we were both responding to my post at the same
time.. I would love the VBA code - though a novice at that. HOWEVER - if
it's easier than completely re-doing my form, I'll be happy to take a stab at
it!

Thank you Gord!
 
G

Gord Dibben

I avoid meged cells like the plague simply due to the many problems caused by
these.

But............................

Here is event code from Greg Wilson.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim NewRwHt As Single
Dim cWdth As Single, MrgeWdth As Single
Dim c As Range, cc As Range
Dim ma As Range

With Target
If .MergeCells And .WrapText Then
Set c = Target.Cells(1, 1)
cWdth = c.ColumnWidth
Set ma = c.MergeArea
For Each cc In ma.Cells
MrgeWdth = MrgeWdth + cc.ColumnWidth
Next
Application.ScreenUpdating = False
ma.MergeCells = False
c.ColumnWidth = MrgeWdth
c.EntireRow.AutoFit
NewRwHt = c.RowHeight
c.ColumnWidth = cWdth
ma.MergeCells = True
ma.RowHeight = NewRwHt
cWdth = 0: MrgeWdth = 0
Application.ScreenUpdating = True
End If
End With
End Sub

This is sheet event code. Right-click on the sheet tab and "View Code".

Copy/paste the above into that sheet module.

As you enter data the rows will autofit.


Gord
 
G

Guest

You (and Greg Wilson)...... BRILLIANT! Worked beautifully!

Thank you for help - and perfect instruction!
 
D

DoubleZ

Hey Gord,

I have a similar problem to the one you addressed in your post. When I
highlight all the rows then select 'format', 'AutoFit Row Height' it will
make the adjustments. However, once new text is added (actually, eveything
in this tab is being controlled by a formula) the rows do not change height
to accomodate the new text. I do have a couple merged cells, but they are at
the very top and are not part of the rows that I am trying to use AutoFit on.
I inserted the VBA code you pasted just in case I have merged cells, but
that didn't help. This is the only other unique thing I can think of that
may be causing it: in the cells of one column I have the values of multiple
texts being returned on separate lines (e.g. one of the cells has the formula
K14&CHAR(10)&L14&CHAR(10)&M14&CHAR(10)&N14).

Can you think of any reason why AutoFit wont work?
 
G

Gord Dibben

Even without merged cells Excel doen't seem to like too many linefeeds and will
not autofit after a certain point.

I tried your example formula in A14 with row set to Autofit.

Entry of text in K14 and L14 autofit but entries in M14 and N14 did not increase
row height.

I don't know of a workaround.


Gord Dibben MS Excel MVP
 
G

Gord Dibben

Private Sub Worksheet_Change(ByVal Target As Range)
Dim NewRwHt As Single
Dim cWdth As Single, MrgeWdth As Single
Dim c As Range, cc As Range
Dim ma As Range
On Error GoTo endit
Application.EnableEvents = False
With Target
If .MergeCells And .WrapText Then
ActiveSheet.Unprotect Password:="justme"
Set c = Target.Cells(1, 1)
cWdth = c.ColumnWidth
Set ma = c.MergeArea
For Each cc In ma.Cells
MrgeWdth = MrgeWdth + cc.ColumnWidth
Next
Application.ScreenUpdating = False
ma.MergeCells = False
c.ColumnWidth = MrgeWdth
c.EntireRow.AutoFit
NewRwHt = c.RowHeight
c.ColumnWidth = cWdth
ma.MergeCells = True
ma.RowHeight = NewRwHt
cWdth = 0: MrgeWdth = 0
Application.ScreenUpdating = True
End If
End With
endit:
Application.EnableEvents = True
ActiveSheet.Protect Password:="justme"
End Sub


Gord
 
B

Brian

Thank you, Gord!

Note that I did have a slight issue where the resized cells were becoming
locked after editing, but I stole a line of code (ma.Locked = False) from
another post, and that seems to solve the issue.

Thanks again.
 
G

Gord Dibben

Good to hear

Thanks for the feedback

Thank you, Gord!

Note that I did have a slight issue where the resized cells were becoming
locked after editing, but I stole a line of code (ma.Locked = False) from
another post, and that seems to solve the issue.

Thanks again.
 
B

brianna3

will this work for data that has already been entered into my sheet or will
this only work on a new sheet and any data entered after it is applied?
 
D

donna-LexusWebs

I added the VBA to an existing worksheet and although it didn't expand my
rows as soon as I added the code, as soon as I typed into the cell again the
row expanded and stayed expanded. I hope that helps!

Thanks for the code - and the specific instructions where to add it (rt-clk
on the tab). I never knew that and always wondered how code was being added!!

Thanks again!
 
G

Gord Dibben

To autofit existing rows after you added the code, select a cell and F2 then
ENTER

This is same as typing in the cell.


Gord
 
J

jonathan edmondson

Hi,

Although this code works with cells where data is actually in it, I need it to work where the cell contains text that has been linked from another sheet.

Can anyone help?

thanks



donna-LexusWebs wrote:

I added the VBA to an existing worksheet and although it did not expand myrows
18-Mar-10

I added the VBA to an existing worksheet and although it did not expand m
rows as soon as I added the code, as soon as I typed into the cell again th
row expanded and stayed expanded. I hope that helps

Thanks for the code - and the specific instructions where to add it (rt-cl
on the tab). I never knew that and always wondered how code was being added!

Thanks again

:

Previous Posts In This Thread:

Autosize cells as data is entered
Hello.

I have my cells set to Wrap & the row is set to Auto-Fit. The "Number" is
set to General

When I add text, it wraps, however, the cell-size is not changing! What do
I have to do to get the cell (or row height) to grow as text is added?

--
Carol

I'm betting you have some "merged cells" which won't Autofit without VBA code.
I am betting you have some "merged cells" which will not Autofit without VBA code

If this is the case and you are interested in the code, post back

Gord Dibben MS Excel MV

wrote:

Nevermind!
Nevermind! Just realized my cells are merged - therefore, none of th
solutions I was trying will work
-
Caro

:

It is the case!
It is the case! I think we were both responding to my post at the same
time.. I would love the VBA code - though a novice at that. HOWEVER - if
it's easier than completely re-doing my form, I'll be happy to take a stab at
it

Thank you Gord
--
Caro

:

I avoid meged cells like the plague simply due to the many problems caused
I avoid meged cells like the plague simply due to the many problems caused b
these

But...........................

Here is event code from Greg Wilson

Private Sub Worksheet_Change(ByVal Target As Range)
Dim NewRwHt As Single
Dim cWdth As Single, MrgeWdth As Single
Dim c As Range, cc As Range
Dim ma As Range

With Target
If .MergeCells And .WrapText Then
Set c = Target.Cells(1, 1)
cWdth = c.ColumnWidth
Set ma = c.MergeArea
For Each cc In ma.Cells
MrgeWdth = MrgeWdth + cc.ColumnWidth
Next
Application.ScreenUpdating = False
ma.MergeCells = False
c.ColumnWidth = MrgeWdth
c.EntireRow.AutoFit
NewRwHt = c.RowHeight
c.ColumnWidth = cWdth
ma.MergeCells = True
ma.RowHeight = NewRwHt
cWdth = 0: MrgeWdth = 0
Application.ScreenUpdating = True
End If
End With
End Su

This is sheet event code. Right-click on the sheet tab and "View Code"

Copy/paste the above into that sheet module

As you enter data the rows will autofit

Gor

You (and Greg Wilson)...... BRILLIANT! Worked beautifully!
You (and Greg Wilson)...... BRILLIANT! Worked beautifully

Thank you for help - and perfect instruction
-
Caro

:

Does anyone know how to modify this code so that it will work in a protected
Does anyone know how to modify this code so that it will work in a protecte
sheet

:

Private Sub Worksheet_Change(ByVal Target As Range)Dim NewRwHt As SingleDim
Private Sub Worksheet_Change(ByVal Target As Range
Dim NewRwHt As Singl
Dim cWdth As Single, MrgeWdth As Singl
Dim c As Range, cc As Rang
Dim ma As Rang
On Error GoTo endi
Application.EnableEvents = Fals
With Target
If .MergeCells And .WrapText Then
ActiveSheet.Unprotect Password:="justme"
Set c = Target.Cells(1, 1)
cWdth = c.ColumnWidth
Set ma = c.MergeArea
For Each cc In ma.Cells
MrgeWdth = MrgeWdth + cc.ColumnWidth
Next
Application.ScreenUpdating = False
ma.MergeCells = False
c.ColumnWidth = MrgeWdth
c.EntireRow.AutoFit
NewRwHt = c.RowHeight
c.ColumnWidth = cWdth
ma.MergeCells = True
ma.RowHeight = NewRwHt
cWdth = 0: MrgeWdth = 0
Application.ScreenUpdating = True
End If
End With
endit:
Application.EnableEvents = True
ActiveSheet.Protect Password:="justme"
End Sub


Gord

Thank you, Gord!
Thank you, Gord!

Note that I did have a slight issue where the resized cells were becoming
locked after editing, but I stole a line of code (ma.Locked = False) from
another post, and that seems to solve the issue.

Thanks again.


:

Re: Autosize cells as data is entered
Good to hear

Thanks for the feedback

wrote:

will this work for data that has already been entered into my sheet or will
will this work for data that has already been entered into my sheet or will
this only work on a new sheet and any data entered after it is applied?

:

Only works when entering new data or editing existing data.
Only works when entering new data or editing existing data.


Gord

I added the VBA to an existing worksheet and although it did not expand myrows
I added the VBA to an existing worksheet and although it did not expand my
rows as soon as I added the code, as soon as I typed into the cell again the
row expanded and stayed expanded. I hope that helps!

Thanks for the code - and the specific instructions where to add it (rt-clk
on the tab). I never knew that and always wondered how code was being added!!

Thanks again!

:


Submitted via EggHeadCafe - Software Developer Portal of Choice
WPF Report Engine, Part 1
http://www.eggheadcafe.com/tutorial...74-4eba5c821311/wpf-report-engine-part-1.aspx
 

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