Loading
Excel.Tips.Net ExcelTips (Menu Interface)

Setting a Length Limit on Cells

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.

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.

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.

Related Tips:

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

 

Leave your own comment:

*Name:
Email:
  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*Text:
*What is 5+3 (To prevent automated submissions and spam.)
 
 
           Commenting Terms

Comments for this tip:

Nick    04 Oct 2016, 00:13
Along the lines of limiting characters in cells, is it possible to allow only 2 characters to be entered in a cell and when the 2nd character is entered - hop over to the next cell on the right?

For example, A1 should only accept numbers 01 through 99. The validation can be done *after the fact* no problem but can it be done AS the user is entering the numbers?

This would make for very quick data entry for my needs.
Binoj Antony    22 Aug 2016, 04:03
I need to limit the charector in one cell to a value. but if it exceeds the value then only texts which is entered up to that value should be there. this is possible?
 
 

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us

 

Advertise with Us

Our Privacy Policy

Our Sites

Tips.Net

Beauty and Style

Cars

Cleaning

Cooking

DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)

Gardening

Health

Home Improvement

Money and Finances

Organizing

Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives

 

Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2016 Sharon Parq Associates, Inc.