Change cell font size based on formula

ColinCoady

New Member
Joined
Mar 21, 2019
Messages
35
I'm look for VBA code to change the font size in a cell based on the number of lines in another cell.
I use the following formula to find out the number of lines in the other cell:
=LEN(InfoEntry!$B$14)-LEN(SUBSTITUTE(InfoEntry!$B$14,CHAR(10),""))+1

If the result of this formula is "8" lines or less the the font size for cell TenantQuote!$A$15 needs to be 12, if 9-14 lines the font size 11 is needed and greater than 14 lines then font size 10 is needed

Can anyone help me with this?
-Colin
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
You could do this with Conditional Formatting no need for VBA.
 
Upvote 0
I am assuming you want this to run on change of the cell so this is a worksheet change code that would go into the infoentry sheet
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B14")) Is Nothing Then
l = Len(Sheets("InfoEntry").Range("$B$14")) - Len(Replace(Sheets("infoentry").Range("$B$14"), vbLf, "")) + 1
Select Case l
    Case Is <= 8
        Sheets("tenantquote").Range("A15").Font.Size = 12
    Case 9 To 14
        Sheets("tenantquote").Range("A15").Font.Size = 11
    Case Else
        Sheets("tenantquote").Range("A15").Font.Size = 10
End Select

End If
End Sub
 
Upvote 0
Looks like that should work but it has no effect.
After testing original I edited your code on the sheet names - ie "tenantquote" to TenantQuote" Wasn't sure if it was case sensitive or not. I'm very new at this.
No affect so i also changed ranges thinking merged cells needed the full address ie $B$14 changed to $B$14:$F$14 but again no effect.

I change the data so the number of lines it calculates change but no effect. The font size remains the same.

Any more thoughts.
Can I send you the workbook?
 
Upvote 0
Where did you put the code?
Right click on the InfoEntry and select view code. That is where it should be.

Note that if you have a formula in B14 the recalculation of the formula will not trigger the event.
 
Upvote 0
You were correct. I combined it with the other one and it's working like a charm.
Thank you Scott T
Great job.
-Colin
 
Upvote 0

Forum statistics

Threads
1,224,747
Messages
6,180,714
Members
452,995
Latest member
isldboy

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