Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to set only 15 characters allowed in excel cell.i use data(DATA Tab) validation in excel to give range of the excel cell.it is working when i type the letters.if i copy more then 15 characters and paste in the cell the validation is not working how to set the validation in excel cell.how to write the macro for excel.

What I have tried:

i use data tab for validation but not working
Posted
Updated 10-Aug-17 2:16am
v2
Comments
Richard MacCutchan 10-Aug-17 7:32am    
Please show us the code of your macro and explain what is not working.

1 solution

Try this....

Create a named range that includes all of the cells that you want to limit to 15 characters. For the code example below I used a named range: "Limit15"

Then, in the "ThisWorkbook" object code window use the "Workbook_SheetChange" event to check the length of the data entered...


VB
Option Explicit

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

    Dim rngLimit As Range
    Set rngLimit = Range("Limit15")
    
    Dim rng As Range
    
    For Each rng In Target  ' there may be more than one cell being changed
        'check if cell changed is part of the named range
        If Not Intersect(rng, rngLimit) Is Nothing Then
        
            'check if the length of the entry is greater than 15
            If Len(rng) > 15 Then
                'give a warning message
                MsgBox "Can't be more than 15 characters...", vbCritical, "Text too long!"
                
                'truncate the text to be 15 characters
                rng = Left(rng, 15)
            End If
        End If
    Next
End Sub
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900