VBA maximum string length within column width

chuf

Well-known Member
Joined
Jun 12, 2006
Messages
619
I'm confused. :confused:

I'm working my way through a series of steps on my current project and I thought this bit would be easy...

i need various columns to be various widths due to the upload requirements of the IT system. The columns will, mostly,contain text.

In the case of column G it will be arrived at by concatenating two other columns of text.

The column width must be 40 and the text MUST be contained within that column width, i.e. no overhanging appearance.

No problem i thought = Len function. But despite the Len being 40, the characters are wider than the 40 width :oops:

I guess i could set the Len to 30 to be "sure" but i'd like to know if there is a VBA solution to set the characters / string to fit within the specific column width.

To summarise: I'd like a VBA code to concate two columns to arrive at text within a maximum column width of 40, deleting the "excess".

Thanks in advance of your help, again :) Chuf
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Why do you need it to be displayed with the specific width? If outputting as prn, for example, the output file will be the width (characters) as specified by the column width setting, not how it displays in the sheet.
 
Upvote 0
Hi Richard,

I agree with your comment, i really do... but the computer says no.

When we've tried in the past to upload the sheet with 40 width and len equal to 40 its failed.

At the moment i've set it to 33 and it appears within the 40.

Maybe the system settings specify 33 but the human interpretation is that its 40 because it appears within 40 width.

as i say confused.com. but my money is on the computer being right :LOL:
 
Upvote 0
Unless you use special fonts characters are not the same width. 40 "g"s will fit in a 40 width Excel column, 40 "m"s won't, and I bet you get more than 40 "i"s in the same cell as well.

Though I honestly am not quite understanding the whole issue.
 
Upvote 0
According to this kb article at Microsoft:

http://support.microsoft.com/kb/214123

What you should do is adjust the Normal style settings (assuming you are using xl2003 or below) to a monospace font like Courier New and then make sure your widths are set and the Len restriction is also set against each cell (don't use Autofit as it will extend the cell's width beyond the number of characters). Your text will then fit perfectly into the cells.

I must admit I'm sceptical that this will fix your issue, but if it does then I guess I shouldn't complain :)
 
Upvote 0
Hi Richard,

thanks very much for searching for that, much appreciated. I read the MS site too, looking for inspiration.

Computer seems to be going loopy then ;)

the IT upload will only accept Courier New 10 so by that reasoning you would expect Len 40 to equal 40 column width... and not hang over the edge .. but it does .. and sending it crashing.

i've set it to 33 now.. i'll try this when i'm back in work and see what happens.

thanks again (y)
 
Upvote 0

Forum statistics

Threads
1,214,907
Messages
6,122,181
Members
449,071
Latest member
cdnMech

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top