changing font size depending on length of string

sammendac

New Member
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.
 

sammendac

New Member
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.
 

Fluff

MrExcel MVP, Moderator
How about
Code:
Sub FontSize()
   Range("A32").Font.size = IIf(Len(Range("A32")) > 20, 10, 12)
End Sub
 

RoryA

MrExcel MVP, Moderator
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.
 

Some videos you may like

This Week's Hot Topics

  • Get External Data (long shot question!)
    This is likely a long shot but I am wondering if it is at all possible for Excel to somehow 'change' the contents of a URL that is being linked to...
  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • Cell Formatting
    Good Morning, I need to format a few different cells in the following manners: A1 has to always add a colon (:) after whatever is typed in by a...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • Workbook_Change stopped working !
    I am working on an app to speed up & automate processing of Credit Cards statements. After data is input from a CSV file, it is presented to the...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...
Top