justincaza
New Member
- Joined
- Dec 15, 2017
- Messages
- 11
Good evening,
I'm hoping someone can assist in creating a VBA code. I'm fairly new at VBA so my apologies.
I'm looking for VBA that is for 1 column, when a user pastes data or enters it freely, data validation should occur. The data that is pasted or typed must be 8 digits, no special characters or spaces, must have the option for leading zeros. If any data validation fails, can the cells be deleted after a warning message pops.
This is what I have so far and it does not seem to be working. I get multiple warning pops for the same cell and I only need 1. I also cannot figure out the delete cell if failed option or leading zero option. I would also like to confirm the space character is not allowed.
I'm hoping someone can assist in creating a VBA code. I'm fairly new at VBA so my apologies.
I'm looking for VBA that is for 1 column, when a user pastes data or enters it freely, data validation should occur. The data that is pasted or typed must be 8 digits, no special characters or spaces, must have the option for leading zeros. If any data validation fails, can the cells be deleted after a warning message pops.
This is what I have so far and it does not seem to be working. I get multiple warning pops for the same cell and I only need 1. I also cannot figure out the delete cell if failed option or leading zero option. I would also like to confirm the space character is not allowed.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Rng As Range
Dim cell As Range
' See if anything entered/copied into column A
Set Rng = Intersect(Target, Range("A$5$:A$5000$"))
' Exit if nothing put in watched column
If Rng Is Nothing Then Exit Sub
' Loop through updated values in watched range
For Each cell In Rng
' See if length equals 8
If Len(cell) <> 8 Then
Application.EnableEvents = False
cell.Value = Left(cell, 8)
Application.EnableEvents = True
MsgBox cell.Address(0, 0) & " Entry Must Equal 8 Digits", _
vbOKOnly, "WARNING!"
If Not Intersect(Range("A:A"), Target) Is Nothing Then
If cell.Value Like "*[0-9]*" Then
MsgBox "No Special Characters Allowed" & cell.Address(0, 0) & "!"
cell.Select
End If
End If
End If
Next cell
End Sub
Last edited by a moderator: