Change font size dynamically in a textbox (not userform textbox)

ajoy123rocks

New Member
Joined
Jul 13, 2022
Messages
9
Office Version
  1. 2019
Platform
  1. Windows
I require a textbox to change the font size of text dynamically based on number of characters in the text

Initially the size is 14
if no. of characters < 20 size = 14
if no. of characters > 20 and < 25, size =13
if no. of characters > 24 and < 30 size = 12
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
ActiveX text box named TextBox1, code in the sheet module:
VBA Code:
Private Sub TextBox1_Change()
    Select Case Len(TextBox1.Value)
        Case 1 To 19
            TextBox1.Font.Size = 14
        Case 20 To 24
            TextBox1.Font.Size = 13
        Case 25 To 29
            TextBox1.Font.Size = 12
    End Select        
End Sub
 
Upvote 0
ActiveX text box named TextBox1, code in the sheet module:
VBA Code:
Private Sub TextBox1_Change()
    Select Case Len(TextBox1.Value)
        Case 1 To 19
            TextBox1.Font.Size = 14
        Case 20 To 24
            TextBox1.Font.Size = 13
        Case 25 To 29
            TextBox1.Font.Size = 12
    End Select       
End Sub
I dont want to use to activeX textbox (as mentioned in the title)..... just normal textbox
 
Upvote 0
Sorry. For a shape text box:
VBA Code:
Public Sub Update_Text_Box_Font_Size(tb As Shape)
    Select Case Len(tb.TextFrame.Characters.Text)
        Case 1 To 19
            tb.TextFrame.Characters.Font.Size = 14
        Case 20 To 24
            tb.TextFrame.Characters.Font.Size = 13
        Case 25 To 29
            tb.TextFrame.Characters.Font.Size = 12
    End Select
End Sub
To change the font size dynamically you would have to call the above from a Worksheet event, for example:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Update_Text_Box_Font_Size Me.Shapes("TextBox 1")
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,879
Messages
6,175,150
Members
452,615
Latest member
bogeys2birdies

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