I would suggest doing this with data validation instead of doing it with code. I'm not sure the menus for Excel 2007, but you can get there by selecting a cell or cells where you want to apply data validation and press alt+D, then press L
From there you can set the condition for data entry into that cell... Use a 'custom' condition and formula like this (presuming validation applied to cell A1):
=AND(LEN(A1)>5,LEN(A1)<13)
Now Excel won't let the user type in something unless it's between 6 and 12 characters. Next, you can use the other tabs in data validation (input message, error alert) to set what messages the user sees. Maybe you would just use the error alert tab and put title = "invalid loan number" and error message = "please try again"
...You could do this with code but in my view it would be less reliable and more difficult to implement. You would then also have to make sure that all of the users always enabled macros, which in itself is more complicated than the solution above. A simple, far from foolproof bit of code to do it though, might look like:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Intersect(Target, Range("A:A")) Is Nothing Then Exit Sub
If Len(Target.Value) < 6 Or Len(Target.Value) > 12 Then
MsgBox "You entered an invalid loan number - please try again.", vbCritical, "Error"
Application.EnableEvents = False 'turn off events, prevent this sub from triggering itself in an endless loop
Target.ClearContents
Application.EnableEvents = True 'turn events on - never leave this off
Target.Select
End If
End Sub
...you would apply that code by right-clicking the tab name, 'view code' and paste in the code. Note that it will fire if you change a cell in column A. You could modify that range to be whatever you would like, including a named range that covers the area where loan numbers are to be entered.
Hope that helps.
Tai