changing font size depending on length of string

sammendac

New Member
Joined
May 28, 2014
Messages
37
Hi

I need to complete a form. one of the fields has a variable length. I need to have the font size reduced if the length of the string is more than 20.

for a len of less than 20 I can use a 12 Pt Font but for more than 20 a 10 Pt font would fit.

I have tried to do this myself but it always goes bananas

Any help would be appreciated.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
When you say a form, do you mean a userform?
 
Last edited:
Upvote 0
No but I am not sure what a userform is.

I take data from our ERP system and produce certificates, such as Certificate of conformity or of Origin. It is typed into a Add data worksheet and then the data is taken to the Certificate sheet. All the fields except the one is question are the same size but the Customer Order No field if of a variable length. If it is over 20 characters it is larger then the cell and doesn't print the full number hence I wish to make it a smaller font to fit.

I tried an If statement on the LEN

similar to

if LEN(range("a32) > 20
large font
else
small font
End if

it's the large/small font I don't seem to be able to get to work.
 
Upvote 0
How about
Code:
Sub FontSize()
   Range("A32").Font.size = IIf(Len(Range("A32")) > 20, 10, 12)
End Sub
 
Upvote 0
If it's just a cell on a worksheet, you could set the Shrink to Fit option on the Alignment tab of the Format Cells dialog.
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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