Characters Must Be in List

wazzulu1

Board Regular
Joined
Oct 4, 2006
Messages
164
Hi;

Is there anyway to modify this code, to check to make sure that a formatted text column cell has 7 characters out of the basic number list (0 to 9), and no alpha characters?

I had to make Column A text, due to some Employee ID numbers containing a lead ZERO, if it's changed to Numeric it will cause a problem.

Presently, this code does not work well with a text formatted column, Pasting in data got around my validations, but I changed the format to allow for that, so now this code hosts a few problems.

1. even though I am forcing a Paste Special Values only, it generated a Run-time error '13': Type mismatch, until I placed this in the code: On Error Resume Next

Now, everything that is pasted in turns red, even using the forced Paste Special.

Folks can either type in entries manually, or they cheat and paste entries in, which made me try to put this together. :(

Private Sub Worksheet_Change(ByVal Target As Range)

'MsgBox Target.Address
If Intersect(Target, Range("A2:A65000")) Is Nothing Then Exit Sub

On Error Resume Next

If Target <> "" Then

Application.EnableEvents = False
Call CleanTrimCells_Looping
Application.EnableEvents = True
If Target <> "" Then
If Len(Target) <> 7 Then
MsgBox ("The Employee ID must be 7 numbers long. Please re-enter a valid Employee ID.")
Target.Font.ColorIndex = 3

ElseIf Not IsNumeric(Target) Then
MsgBox ("The Employee ID must be entered using numbers only. Please re-enter a valid Employee ID.")
Target.Font.ColorIndex = 5
Exit Sub





End If
End If
End If

End Sub
**************************************************************************************
Sub CheckValidation()
Dim cell As Range, bErr As Boolean

For Each cell In ActiveSheet.UsedRange
If Not cell.Validation.Value Then
cell.Select
bErr = True
MsgBox cell.Address(False, False) & " fails validation criteria"
End If
Next
If Not bErr Then MsgBox "All cells OK"
End Sub
*********************************************************************************************
Sub CleanTrimCells_Looping()

Dim cell As Range
Dim lastRow As Long
Dim rng As Range

Application.EnableEvents = False

'Weed out any formulas from selection
If Selection.Cells.Count = 1 Then
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
Set rng = Range("A2:A" & lastRow)
Else
Set rng = Selection.SpecialCells(xlCellTypeConstants)
End If

'Trim and Clean cell values
For Each cell In rng.Cells
cell.Value = Application.WorksheetFunction.Clean(Application.WorksheetFunction.Trim(cell.Value))
'cell.Value = Application.WorksheetFunction.Trim(cell.Value)
Next cell

Application.EnableEvents = True

End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
I called it cheating, but I work for a Company, I don't own it :(, so I can't force them to type in hundreds of entries manually.
 
Upvote 0
you can get rid of spaces (TRIM), test for LEN of 7 and ISNUMBER to make sure it might be valid
 
Upvote 0
I did not see a clear way to accomplish this through data validation, how can it ensure that it's a 7 character string, made of only 0,1,2,3,4,5,6,7,8,or 9, even if values are pasted in?
 
Upvote 0
The Column has to be text, because some employees have a lead zero in their employee id number, so using ISNUMBER won't work.
 
Upvote 0
I called it cheating, but I work for a Company, I don't own it :(, so I can't force them to type in hundreds of entries manually.

If the "guys" must be allowed to do copy/paste i think you should create a macro, assigned to a button, where they select the range to be copied and in the macro you check each value to ensure they fit the validation rule.

M.
 
Upvote 0
I have forced them to use Paste Special using VBA, and modifying the spreadsheet through XML, but I am struggling trying to find a means to check the characters of the string in the way I described it using VBA Code.
 
Upvote 0
I am using Excel 2010.

Code:
[COLOR=#333333]Private Sub Worksheet_Change(ByVal Target As Range)[/COLOR]

[COLOR=#333333]'MsgBox Target.Address[/COLOR]
[COLOR=#333333]If Intersect(Target, Range("A2:A65000")) Is Nothing Then Exit Sub[/COLOR]

[COLOR=#333333]On Error Resume Next[/COLOR]

[COLOR=#333333]If Target <> "" Then[/COLOR]

[COLOR=#333333]Application.EnableEvents = False[/COLOR]
[COLOR=#333333]Call CleanTrimCells_Looping[/COLOR]
[COLOR=#333333]Application.EnableEvents = True[/COLOR]
[COLOR=#333333]If Target <> "" Then[/COLOR]
[COLOR=#333333]If Len(Target) <> 7 Then[/COLOR]
[COLOR=#333333]MsgBox ("The Employee ID must be 7 numbers long. Please re-enter a valid Employee ID.")[/COLOR]
[COLOR=#333333]Target.Font.ColorIndex = 3[/COLOR]

[COLOR=#333333]ElseIf Not IsNumeric(Target) Then[/COLOR]
[COLOR=#333333]MsgBox ("The Employee ID must be entered using numbers only. Please re-enter a valid Employee ID.")[/COLOR]
[COLOR=#333333]Target.Font.ColorIndex = 5[/COLOR]
[COLOR=#333333]Exit Sub[/COLOR]





[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]End If[/COLOR]

[COLOR=#333333]End Sub[/COLOR]
[COLOR=#333333]**************************************************************************************[/COLOR]
[COLOR=#333333]Sub CheckValidation()[/COLOR]
[COLOR=#333333]Dim cell As Range, bErr As Boolean[/COLOR]

[COLOR=#333333]For Each cell In ActiveSheet.UsedRange[/COLOR]
[COLOR=#333333]If Not cell.Validation.Value Then[/COLOR]
[COLOR=#333333]cell.Select[/COLOR]
[COLOR=#333333]bErr = True[/COLOR]
[COLOR=#333333]MsgBox cell.Address(False, False) & " fails validation criteria"[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]Next[/COLOR]
[COLOR=#333333]If Not bErr Then MsgBox "All cells OK"[/COLOR]
[COLOR=#333333]End Sub[/COLOR]
[COLOR=#333333]*********************************************************************************************[/COLOR]
[COLOR=#333333]Sub CleanTrimCells_Looping()[/COLOR]

[COLOR=#333333]Dim cell As Range[/COLOR]
[COLOR=#333333]Dim lastRow As Long[/COLOR]
[COLOR=#333333]Dim rng As Range[/COLOR]

[COLOR=#333333]Application.EnableEvents = False[/COLOR]

[COLOR=#333333]'Weed out any formulas from selection[/COLOR]
[COLOR=#333333]If Selection.Cells.Count = 1 Then[/COLOR]
[COLOR=#333333]lastRow = Cells(Rows.Count, "A").End(xlUp).Row[/COLOR]
[COLOR=#333333]Set rng = Range("A2:A" & lastRow)[/COLOR]
[COLOR=#333333]Else[/COLOR]
[COLOR=#333333]Set rng = Selection.SpecialCells(xlCellTypeConstants)[/COLOR]
[COLOR=#333333]End If[/COLOR]

[COLOR=#333333]'Trim and Clean cell values[/COLOR]
[COLOR=#333333]For Each cell In rng.Cells[/COLOR]
[COLOR=#333333]cell.Value = Application.WorksheetFunction.Clean(Application.WorksheetFunction.Trim(cell.Value))[/COLOR]
[COLOR=#333333]'cell.Value = Application.WorksheetFunction.Trim(cell.Value)[/COLOR]
[COLOR=#333333]Next cell[/COLOR]

[COLOR=#333333]Application.EnableEvents = True[/COLOR]

[COLOR=#333333]End Sub[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,214,945
Messages
6,122,397
Members
449,081
Latest member
JAMES KECULAH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top