Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 81
Default Font adjusts to size of cell

I wondering if anyone has any ideas on automatically changing the size
of a font to adjust to the size of a cell. I'm thinking something
like...

If Length of cell contents < x then adjust font to fit on one line
If Length of cell contents x but then adjust font to maximize on two
lines

All, of course, without increase the row height.

Thanks

- John

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 128
Default Font adjusts to size of cell

Don't really know what you mean but you could

click format cells alignment tick the shrink to fit option

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...mming/200602/1
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 81
Default Font adjusts to size of cell

It seems that shrink to fit and wrap text are mutually exclusive. I
need to be able to allow shrinking to fit but in a cell that allows to
lines of text.

- John

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Font adjusts to size of cell

Hi John,

This is only lightly tested but have a go

Function AutoSizeFont(rng As Range)
Dim ndRh As Double, ndFntSze As Double
Dim cel As Range
Dim rDoItIn As Range

' Fit text in cell to existing cell width & height

On Error GoTo errH
Application.ScreenUpdating = False
With rng.Parent.UsedRange
'error if UR extends to end row or column
Set rDoItIn = rng.Parent.Cells(.Rows.Count + _
.Rows(1).Row, .Columns.Count + .Columns(1).Column)
End With

rng.WrapText = True
rng.VerticalAlignment = xlCenter

For Each cel In rng
ndRh = cel.Height
With rDoItIn

.RowHeight = ndRh
.ColumnWidth = cel.ColumnWidth
.Value = cel.Value
.WrapText = True

With .Font
.Size = 4

Do
ndFntSze = .Size
.Size = ndFntSze + 0.25
rDoItIn.Rows(1).AutoFit
Loop Until rDoItIn.Height = ndRh

If rDoItIn.Height ndRh Then
cel.Font.Size = ndFntSze
Else
cel.Font.Size = .Size
End If

End With
End With
Next
rng.Parent.Rows(rDoItIn.Rows(1).Row).EntireRow.Del ete

errH:
Application.ScreenUpdating = True

If Err.Number Then
MsgBox "Error"
End If

End Function

Regards,
Peter T


"John Michl" wrote in message
oups.com...
I wondering if anyone has any ideas on automatically changing the size
of a font to adjust to the size of a cell. I'm thinking something
like...

If Length of cell contents < x then adjust font to fit on one line
If Length of cell contents x but then adjust font to maximize on two
lines

All, of course, without increase the row height.

Thanks

- John



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 81
Default Font adjusts to size of cell

Thanks Peter. I'll give it a spin.

- John



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Font adjusts to size of cell

Function as posted should be considered as beta v1.0 !

I should have explained how it works
Get a temporary cell (rDoItIn) one below & right of the usedrange in which
to adjust font.
Put in the text and fix dimensions to same as the looped input cells.
Gradually increase font size and autofit the row until height is same as
input cell.
WrapText to enable multiple rows.
Resize input cell's font.

Needs more refining, eg

- restrict input cells to those in the used range
- ensure font name is consistent
- even with text wrap, the longest word might extend to the right of the
column when resizing to fit row height (also need to autofit column-width)

I'll leave it for a while in case you (John) or anyone else chime in with
anything else that needs fixing.

Peter T



"Peter T" <peter_t@discussions wrote in message
...
Hi John,

This is only lightly tested but have a go

Function AutoSizeFont(rng As Range)
Dim ndRh As Double, ndFntSze As Double
Dim cel As Range
Dim rDoItIn As Range

' Fit text in cell to existing cell width & height

On Error GoTo errH
Application.ScreenUpdating = False
With rng.Parent.UsedRange
'error if UR extends to end row or column
Set rDoItIn = rng.Parent.Cells(.Rows.Count + _
.Rows(1).Row, .Columns.Count + .Columns(1).Column)
End With

rng.WrapText = True
rng.VerticalAlignment = xlCenter

For Each cel In rng
ndRh = cel.Height
With rDoItIn

.RowHeight = ndRh
.ColumnWidth = cel.ColumnWidth
.Value = cel.Value
.WrapText = True

With .Font
.Size = 4

Do
ndFntSze = .Size
.Size = ndFntSze + 0.25
rDoItIn.Rows(1).AutoFit
Loop Until rDoItIn.Height = ndRh

If rDoItIn.Height ndRh Then
cel.Font.Size = ndFntSze
Else
cel.Font.Size = .Size
End If

End With
End With
Next
rng.Parent.Rows(rDoItIn.Rows(1).Row).EntireRow.Del ete

errH:
Application.ScreenUpdating = True

If Err.Number Then
MsgBox "Error"
End If

End Function

Regards,
Peter T


"John Michl" wrote in message
oups.com...
I wondering if anyone has any ideas on automatically changing the size
of a font to adjust to the size of a cell. I'm thinking something
like...

If Length of cell contents < x then adjust font to fit on one line
If Length of cell contents x but then adjust font to maximize on two
lines

All, of course, without increase the row height.

Thanks

- John





Reply
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
When I change the font size in a cell the value changes. Value increases with increase in font. Excel Worksheet Functions 5 June 28th 07 11:00 PM
cell size and font size Aamir Excel Discussion (Misc queries) 1 October 10th 06 05:51 PM
VBAcustom footercell ref + font size Adam Molinaro Excel Programming 4 October 6th 05 03:56 PM
How to sent for cell: font size=7,text format in VBA netx Excel Discussion (Misc queries) 1 August 8th 05 06:50 PM
change font size and bold in cell? R Doornbosch Excel Programming 7 February 10th 04 12:03 AM


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