I have one excel sheet where user will enter meta data in each cell. What I need to do is write a macro which will allow only alpha numeric characters and comma.
I wrote a macro for test purpose which wouldn’t allow special characters. But after I entered one valid character in cell it’s allowing my disallowed characters. I don’t know how to rectify it.
My code:
Private Sub WorkBook_Open()
MsgBox "Running the Disable_Keys() Macro"
Call ThisWorkbook.Disable_Keys
End Sub
Sub MyMsg()
MsgBox "Press Another Key"
End Sub
Sub Disable_Keys()
Dim I As Long
Dim KeysArray As Variant
Dim Key As Variant
KeysArray = Array("@", "!", "~", "#", "$", "&", "|", "\", ":", "*", "_", "-", "=", "'", ";", "<", ">", "?", "/", "'", ":")
For Each Key In KeysArray
Application.OnKey Key, "ThisWorkbook.MyMSg"
Next Key
End Sub
This will check for if there is something that is not 0-9,a-z or a comma
If you want to exclude specific characters, then the
.Patternwould be what you changeIf it finds something, then the cell is invalid, and it will clear the cell.