Limit number of characters in a cell
This tutorial shows how to allow to only enter n number of characters in a cell using Excel or VBA
Select a cell > Data tab > Data Tools group > Click on Data Validation > Data Validation > Select Settings tab > Select Text length > Select equal to > Enter number > Click OK
1. Select a cell in which you want to limit users to only be able to enter a specific number of characters. Note: in this example we are selecting cell B2. |
2. Select the Data tab. |
3. Click on Data Validation in the Data Tools group. 4. Select Data Validation. |
5. Select the Settings tab. 6. Select Text length in the Allow input box. 7. Select equal to in the Data input box. 8. Enter the number of characters you want a user to be able to enter in a selected cell. 9. Click OK Note: in this example we are applying a condition that will only allow you to enter five characters in a selected cell. |
METHOD 2. Limit number of characters in a cell using a formula
EXCEL
Select a cell > Data tab > Data Tools group > Click on Data Validation > Data Validation > Select Settings tab > Select Custom > Enter formula > Click OK
1. Select a cell in which you want to limit users to only be able to enter a specific number of characters. Note: in this example we are selecting cell B2. |
2. Select the Data tab. |
3. Click on Data Validation in the Data Tools group. 4. Select Data Validation. |
5. Select the Settings tab. 6. Select Custom in the Allow input box. 7. Enter the formula =LEN(B2)=5 in the Formula input box. 8. Click OK Note: in this example we are applying a condition that will only allow you to enter five characters in a selected cell. The selected cell being B2. |
METHOD 1. Limit number of characters in a cell
VBA
Sub Limit_number_of_characters()
'declare variables
Dim ws As Worksheet
Dim Rng As Range
Dim ws As Worksheet
Dim Rng As Range
Set ws = Worksheets("Analysis")
Set Rng = ws.Range("B2")
Set Rng = ws.Range("B2")
'apply data validation
With Rng.Validation
With Rng.Validation
.Add Type:=xlValidateTextLength, Operator:=xlEqual, Formula1:="5"
End With
End Sub
ADJUSTABLE PARAMETERS
Worksheet Selection: Select the worksheet in which you want to apply a limit of the number of characters that can be entered into a cell by changing the Analysis worksheet name in the VBA code. You can also change the name of this object variable, by changing the name 'ws' in the VBA code.
Cell: Select the cell in which you want to apply a limit of the number of characters that can be entered into it by changing the cell reference ("B2") in the VBA code.
Number of Characters: Select the number of characters that can only be entered in a cell by changing the number "5" in the VBA code.
Worksheet Selection: Select the worksheet in which you want to apply a limit of the number of characters that can be entered into a cell by changing the Analysis worksheet name in the VBA code. You can also change the name of this object variable, by changing the name 'ws' in the VBA code.
Cell: Select the cell in which you want to apply a limit of the number of characters that can be entered into it by changing the cell reference ("B2") in the VBA code.
Number of Characters: Select the number of characters that can only be entered in a cell by changing the number "5" in the VBA code.
EXPLANATION
This tutorial shows how to allow to only enter n number of characters in a cell using Excel or VBA. Therefore, Excel will not allow you to enter more or less characters than the number that you have specified. In this example we are only allowing users to enter five character in a selected cell.
This tutorial provides two Excel methods that can be applied to limit the number of characters that can be entered in a cell. The first method uses the Excel built-in data validation option (equal to) and can be completed in nine steps. The second method is achieved through the use of a formula in the Data Validation dialog box and can be completed in eight steps.
The VBA code in this tutorial uses a Validation function with the xlValidateTextLength validation type to limit the number of characters that can be entered in a cell.
RELATED TOPICS
Related Topic | Description | Related Topic and Description |
---|---|---|
Limit maximum number of characters in a cell | How to limit users to only be able to enter up to a specific number of characters in a cell | |
Limit minimum number of characters in a cell | How to limit a minimum number of characters that can be entered in a cell |