Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Setting a Length Limit on Cells.

Setting a Length Limit on Cells

Written by Allen Wyatt (last updated January 22, 2022)
This tip applies to Excel 97, 2000, 2002, and 2003


Craig is developing a worksheet and wants to know if there is a way to specify the maximum number of characters that can be entered in any given cell. He doesn't want to use Data Validation to impose the limitation.

There is no way to do this directly in Excel without (as Craig mentions) using Data Validation. There are a few things you can try to achieve the desired effect, however. First, you can using a formula to check the length of any cell, and then display an error message, if desired. For instance, if the cells you want to check are in column C, you could use a formula such as the following:

=IF((LEN(C1)>15),"Cell is Too Long","")

Place the formula in the cell to the right of the cell being checked (such as in cell D1), and then copy it down as many cells as necessary. When an entry is made in C1, and if it is more than 15 characters, then the message is displayed.

If such a direct approach is undesirable, then you'll need to use macros to do the checking. The following is a simple example that is triggered whenever something is changed in the worksheet. Each cell in the worksheet is then checked to make sure it is not longer than 15 characters. If such a cell is discovered, then a message box is displayed and the cell is cleared.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    For Each cell In UsedRange
        If Len(cell.Value) > 15 Then
            MsgBox " Can't enter more than 15 characters"
            cell.Value = ""
        End If
    Next
End Sub

A more robust approach is to check in the event handler to see if the change was made somewhere within a range of cells that need to be length-limited.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    Dim rng As Range
    Dim rCell As Range
    Dim iChars As Integer
    On Error GoTo ErrHandler

    'Change these as desired
    iChars = 15
    Set rng = Me.Range("A1:A10")

    If Not Intersect(Target, rng) Is Nothing Then
        Application.EnableEvents = False
        For Each rCell In Intersect(Target, rng)
            If Len(rCell.Value) > iChars Then
                rCell.Value = Left(rCell.Value, iChars)
                MsgBox rCell.Address & " has more than" _
                  & iChars & " characters." & vbCrLf _
                  & "It has been truncated."
            End If
        Next
    End If

ExitHandler:
    Application.EnableEvents = True
    Set rCell = Nothing
    Set rng = Nothing
    Exit Sub

ErrHandler:
    MsgBox Err.Description
    Resume ExitHandler
End Sub

To use this macro, you simply need to change the value assigned to iChars (represents the maximum length allowed) and the range assigned to rng (currently set to A1:A10). Because the macro checks only for changes within the specified range, it is much faster with larger worksheets than the macro that checks all the cells used.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3150) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Setting a Length Limit on Cells.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Removing Borders

Need to get rid of the borders around a cell? The shortcut in this tip can make quick work of this formatting task.

Discover More

Problem Printing Quotation Marks

If you go to print a document and find out that your quotation marks aren't printing properly, there could be a number of ...

Discover More

Changing Chart Location

Charts can either be embedded in a worksheet or take up an entire sheet by themselves. Changing from one type of chart to ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (menu)

Shortcut for Selecting a Data Range

Want to select all the data in a contiguous section of a worksheet? The shortcut discussed in this tip makes it very easy.

Discover More

Contingent Validation Lists

Data validation can be used to create lists of choices for entry into a particular cell. Using the techniques in this tip ...

Discover More

Ensuring Rows and Columns are Empty

Before you go about deleting rows and columns helter-skelter, it is a good idea to determine if there is anything in the ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is one less than 9?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.