This will work (in 2003 ... of course, in 2002).
With Columns("B:B") oldWidth = .ColumnWidth .EntireColumn.AutoFit ' Aha! fitWidth = .ColumnWidth .ColumnWidth = oldWidth ' Restore original width If oldWidth < fitWidth Then ' Text is too wide for column. ' Do stuff. End If End With
I would not get bogged down in this appraisal business if I were to you ... It just asked for unexpected things.
EDIT. Addressing points in your comment.
The above will automatically assign the column to the cell with the widest text. To view only the text in the current cell, copy the cell text into some empty column and execute AutoFit.
If you are concerned about performance, you will have to bite the bullet and compile a table of character widths. Loop through Chr(i) and measure the width of each character using the above technique. Save the results in an array, create a function to get the width of the string by looking at the width of the char in that array and adding up. The initial cost of creating a LUT will be started, but the search will be very fast, unnoticed. Of course, the LUT will only be valid for the current font, so I will recreate it every time I run the code. It is worth it if there are as many lines as you say.
NB: fitWidth above will return the width of the PLUS symbol a small value (0.29pt on my machine), which is a kind of thin “white box” automatically added on both sides of the cell for aesthetic purposes. Remember to subtract this from fitWidth to get the true character width. You can find out how wide it is by doing auto-tuning on “A” and “AA”. The difference between them will be the true width of one "A".
source share