Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default String width not exceed column width

Given the font and size (Arial, 10), is it possible to calculate how wide a
given string will be? I have 120 columns that are 1 character wide (12
pixels). I would like to know how many columns a given string will cover.

Thank You!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default String width not exceed column width

Difficult for a proportional font.
--
Gary''s Student - gsnu200750


"Norm Lundquist" wrote:

Given the font and size (Arial, 10), is it possible to calculate how wide a
given string will be? I have 120 columns that are 1 character wide (12
pixels). I would like to know how many columns a given string will cover.

Thank You!

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,092
Default String width not exceed column width

LEN(string) will give the number of characters in the string (includes
spaces). Since each column is one character wide, then this formula would be
real close to the actual number of columns used. Keep in mind not all
characters are the same width.

Mike F
"Norm Lundquist" wrote in message
...
Given the font and size (Arial, 10), is it possible to calculate how wide
a
given string will be? I have 120 columns that are 1 character wide (12
pixels). I would like to know how many columns a given string will cover.

Thank You!



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default String width not exceed column width

See if this site helps you:

http://office.microsoft.com/en-us/ex...CL100570551033

"Norm Lundquist" wrote:

Given the font and size (Arial, 10), is it possible to calculate how wide a
given string will be? I have 120 columns that are 1 character wide (12
pixels). I would like to know how many columns a given string will cover.

Thank You!

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,092
Default String width not exceed column width

However, I just did a test and 69 characters used 33 columns. As Gary's
Student said, a really difficult answer.

Mike F
"Mike Fogleman" wrote in message
m...
LEN(string) will give the number of characters in the string (includes
spaces). Since each column is one character wide, then this formula would
be real close to the actual number of columns used. Keep in mind not all
characters are the same width.

Mike F
"Norm Lundquist" wrote in
message ...
Given the font and size (Arial, 10), is it possible to calculate how wide
a
given string will be? I have 120 columns that are 1 character wide (12
pixels). I would like to know how many columns a given string will
cover.

Thank You!







  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default String width not exceed column width

In theory it's possible to calculate the width of each known character,
cater for mixed formats, etc. But the easiest way is to copy the text to a
suitably formatted cell in an otherwise empty column, perhaps in a hidden
sheet. Then do an Autofit and check the column width.

It's also possible to do similar in a Textbox, if anything slightly more
accurate as cells have a little padding which varies according to xl
version, but much more code.

Sub test4()
Dim rTmp As Range
Set rTmp = Worksheets("Sheet3").Range("A1")
rTmp(1).EntireColumn.ClearContents

rTmp.Font.Name = "Arial"
rTmp.Font.Size = 10
rTmp.Value = "Some text of unknown width"
rTmp(1).EntireColumn.AutoFit

MsgBox rTmp(1).EntireColumn.Width

End Sub

Regards,
Peter T


"Norm Lundquist" wrote in message
...
Given the font and size (Arial, 10), is it possible to calculate how wide

a
given string will be? I have 120 columns that are 1 character wide (12
pixels). I would like to know how many columns a given string will cover.

Thank You!



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default String width not exceed column width

Thanks, Peter! Autofitting the column will get me close enough and any extra
padding is no problem. Nice, simple solution ... let Excel do the work!



"Peter T" wrote:

In theory it's possible to calculate the width of each known character,
cater for mixed formats, etc. But the easiest way is to copy the text to a
suitably formatted cell in an otherwise empty column, perhaps in a hidden
sheet. Then do an Autofit and check the column width.

It's also possible to do similar in a Textbox, if anything slightly more
accurate as cells have a little padding which varies according to xl
version, but much more code.

Sub test4()
Dim rTmp As Range
Set rTmp = Worksheets("Sheet3").Range("A1")
rTmp(1).EntireColumn.ClearContents

rTmp.Font.Name = "Arial"
rTmp.Font.Size = 10
rTmp.Value = "Some text of unknown width"
rTmp(1).EntireColumn.AutoFit

MsgBox rTmp(1).EntireColumn.Width

End Sub

Regards,
Peter T


"Norm Lundquist" wrote in message
...
Given the font and size (Arial, 10), is it possible to calculate how wide

a
given string will be? I have 120 columns that are 1 character wide (12
pixels). I would like to know how many columns a given string will cover.

Thank You!




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
Change Cell Width WITHOUT changing Column width dww701 Excel Discussion (Misc queries) 1 January 12th 09 10:52 PM
Create a macro which takes a column name and width and sets the column width to what it should be Ag Excel Programming 4 September 29th 07 11:29 PM
How to make cell width different than the column width it lies in John Excel Discussion (Misc queries) 2 September 11th 06 10:41 PM
Set Column Width Based On Total Width Of Other Columns rayneraingoaway Excel Programming 1 June 28th 06 11:10 PM


All times are GMT +1. The time now is 10:34 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"