View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Hank Scorpio Hank Scorpio is offline
external usenet poster
 
Posts: 103
Default Change Event.....Cell character count

On Fri, 30 Jan 2004 21:07:14 -0500, "CLR"
wrote:

Hi All..........

Is it possible to have a Change Event macro count how many characters have
just been typed in the cell and if over 256, put up a Error Message
specifying how many characters need to be reduced, in order for all to be
displayed?


I don't know whether you meant "just typed" literally (as in, if the
user had edited a pre-existing entry, for example), or whether you
just mean the number of characters entered in the cell. If it's the
latter, the following seemed to work for me (regardless of whether the
entry was text or numeric):

Private Sub Worksheet_Change(ByVal Target As Range)

Dim l As Long

l = Len(Target.Value)

If l 256 Then
Beep

MsgBox "Too many characters! (256 maximum.) " _
& "You need to reduce the cell entry " _
& "by " & l - 257 _
& IIf(l - 257 = 1, " character.", " characters"), _
vbCritical

Target.Select

End If

End Sub

If it's the former, I doubt that you could do it short of storing the
original text in a global variable (perhaps on the Selection Change
event) and then comparing that with the resulting text after the
Change event occurs. Hopefully that's not what you were getting at,
though.

---------------------------------------------------------
Hank Scorpio
scorpionet who hates spam is at iprimus.com.au (You know what to do.)
* Please keep all replies in this Newsgroup. Thanks! *