How to limit a userform textbox to a minimum of 18 characters?

danbates

Active Member
Joined
Oct 8, 2017
Messages
377
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have a textbox1 within a userform that I would like to limit to a minimum of 18 characters.

If the textbox1 has less than 18 then I would like a message box to say so.

Any help would be much appreciated.

Kind Regards

Dan
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hello Dan,

The TextBox has a property MaxLength. To limit the maximum number of characters to 18, set the property either at Design time or at run time. If you decide to set the length at runtime, you can do it in the UserForm Initialize event.

To alert the user there are not enough characters, use the TextBox AfterUpdate event and the TextBox Exit event. A boolean value InputError is checked in the Exit event to display a message to the user there are not enough characters were input.

Add this to the Declarations Section of your UserForm
Code:
Dim InputError As Boolean

Add these event macros to your UserForm for TextBox1.
Code:
Private Sub TextBox1_AfterUpdate()
        InputError = False
        If Len(TextBox1) = 0 Then Exit Sub
        
        If Len(TextBox1) < TextBox1.MaxLength Then
            InputError = True
            MsgBox "You did not enter enough characters."
            Exit Sub
        End If
End Sub

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        If InputError = True Then
            Cancel = True
            Password = ""
            TextBox1.Value = ""
        End If
End Sub

Private Sub UserForm_Initialize()
    TextBox1.MaxLength = 18
End Sub
 
Upvote 0
Hi Leith,

Thank you for your response and sorry for the late reply.

I've added your code and it works fine but I was wondering if its possible to change this part please?

Code:
TextBox1.MaxLength = 18

Instead of having it a maximum of 18 characters could I have it so its a minimum of 18 characters please?

Kind Regards

Dan
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,279
Members
452,630
Latest member
OdubiYouth

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