ghudson
Registered User.
- Local time
- Today, 15:44
- Joined
- Jun 8, 2002
- Messages
- 6,195
I am trying to prevent a user from keying spaces, quotes and double quotes in specific columns. I have my code
working to where I am able to replace the invalid characters but I want to warn the user with a message box
each time I have to correct their invalid text entries.
Where or how do I place my message box so that the user is only warned after they commit the error? My
current code below is triggering the message box not matter if the user keys the invalid characters or not.
Can you help me with how to only display the error message when the user has keyed an invalid text? I am also
open to any suggestions on how else to code my routine if there is a more efficient way to do it versus how I
have it written. I did a lot of searching and this was the results of my efforts. I am using Excel 2003. Thanks!
working to where I am able to replace the invalid characters but I want to warn the user with a message box
each time I have to correct their invalid text entries.
Where or how do I place my message box so that the user is only warned after they commit the error? My
current code below is triggering the message box not matter if the user keys the invalid characters or not.
Can you help me with how to only display the error message when the user has keyed an invalid text? I am also
open to any suggestions on how else to code my routine if there is a more efficient way to do it versus how I
have it written. I did a lot of searching and this was the results of my efforts. I am using Excel 2003. Thanks!
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Err_Worksheet_Activate
If Target.Column = 2 Then 'column B, all cells in the column
Application.EnableEvents = False
Target = Replace(Target, " ", "")
MsgBox "You keyed an invalid space in cell " & Target.Offset(0, 0).Address(False, False) & ". Your space was removed."
Application.EnableEvents = True
End If
If Target.Column = 4 Then 'column D, all cells in the column
Application.EnableEvents = False
Target = Replace(Target, """", "IN")
MsgBox "You keyed an invalid double quote in cell " & Target.Offset(0, 0).Address(False, False) & ". Your double quote was converted into " & "''IN'' for inches."
Application.EnableEvents = True
End If
Exit_Worksheet_Activate:
Application.EnableEvents = True
Exit Sub
Err_Worksheet_Activate:
MsgBox Err.Number & " - " & Err.Description
Resume Exit_Worksheet_Activate
End Sub