I have a report generator in Excel VBA, which works so far. It reads some input data and produces a formatted Excel sheet as result. The last column of this sheet is filled with some kind of free text.
Now, sometimes the free text does not fit into the last column, it is just too wide. Since the row height of all rows of this report is fixed, I cannot set range.WrapText=True for this column (to let the text stay visible, one would have to increase the row height). A manual (non-VBA) solution is easy: split the text into several parts and spread it over different rows. For example:
A | B |C
content |This text is too wide for column B.
here |This text fits. |
is | |
fixed | |
should be transformed into
A | B |C
content |This text is too |
here |wide for column |
is |B. |
fixed |This text fits. |
I could easily code that if I would be able to determine the real text width (using a proportional font!) of the content in column B using VBA. range.ColumnWidth gives me the actual width of the column, but I have no idea how I determine the width of "This text is too wide for column B." in VBA. Any suggestions?
(I am currently using Excel 2002 / XP).
This will work (in 2003… surely in 2002 as well).
I wouldn’t get bogged down in this estimation business if I were you… That’s just asking for unexpected stuff to happen.
EDIT: Addressing points in your comment.
The above will AutoFit the column to the cell with the widest text. To consider only the text in the current cell, copy cell text into some blank column and do the AutoFit there.
If you’re concerned about performance, then you’ll have to bite the bullet and make a look-up table of character widths. Cycle through
Chr(i)and measure the width of each character, using the technique above. Store results in an array, make a function to get width of a string by looking up char widths in that array and summing. There will be an initial cost for making the LUT, but look-ups will be very quick, unnoticeably so. Of course, the LUT will only be valid for the current font, so I’d re-create it every time the code is run. Worth the cost if there are as many rows as you say.NB:
fitWidthabove will return the width of the character PLUS some small value (0.29pt on my machine) which is a kind of thin “white margin” automatically added on both sides of the cell for aesthetic purposes. Remember to subtract that fromfitWidthto get the true character width. You can figure out how wide that is by doing an autofit on “A” and “AA”. The difference between these will be the true width of a single “A”.