Limit the amount of text in a textbox

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,153
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Can anyone please tell me how to limit the amount of text that can be put into a textbox? For example if I only want 10 words maximum thats it.

Thanks in advance
Stephen
 

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.
Here is one way to accomplish that, assuming that words are defined by the virtue of a space separating them. Modify the code for TextBox name if it is not TextBox1.

Private Sub TextBox1_Change()
Dim Spaces As Integer
With TextBox1
Spaces = Len(.Text) - Len(Application.Substitute(.Text, Chr(32), ""))
If Spaces < 10 Then Exit Sub
Application.EnableEvents = False
.Text = Trim(.Text)
Application.EnableEvents = True
End With
MsgBox "You are limited to 10 words.", 48, "Too many words."
End Sub
 
Upvote 0
Is this a textbox on a useform or worksheet?

What version of Excel are you using?

Something like this might work in Excel 2000.

Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim arrSentence

    arrSentence = Split(TextBox1.Text, " ")
    
    If UBound(arrSentence) > 9 Then
        MsgBox "That's more than 10 words"
    End If
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,206,759
Messages
6,074,773
Members
446,086
Latest member
daywi

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