
Tips.Net > ExcelTips Home > Editing > Setting a Length Limit on Cells
Summary: When creating a worksheet for others, you may want to limit the amount of information that can be entered in a cell. There are a couple of different ways you can approach the problem, but the best method may be with the macros presented in this tip. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, Excel 2003, and Excel 2007.)
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.
Tip #3150 applies to Microsoft Excel versions: 97 2000 2002 2003 2007
More Power! For some people, the prospect of creating macros can be scary. Those who conquer their fears, however, find they become much more confident and productive once they learn how to make Excel do exactly what they want. ExcelTips: The Macros is an invaluable source for learning Excel macros. You are introduced to the topic in bite-sized chunks, pulled from past issues of ExcelTips. Learn at your own pace, exactly the way you want.
Check out ExcelTips: The Macros today!
PivotTables don't need to be scary or mysterious. Use this powerful tool to analyze your data in ways you didn't know were possible. (more information...)
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site