I have a spreadsheet where data is pasted on from spreadsheet supplied by various external companies. One of the main problems we're having when validating the data is an incorrect NI Number and I'm hoping to find a way of getting this automated.
What I would like to do is, when a block of text is pasted into our template, a block of code to look through the selection and check column S for a valid format NI no (eg AB123456C). I have some code (below) that works when changing an individual cell, but nothing that works when pasting in blocks of text.
Can anyone offer some advice?
What I would like to do is, when a block of text is pasted into our template, a block of code to look through the selection and check column S for a valid format NI no (eg AB123456C). I have some code (below) that works when changing an individual cell, but nothing that works when pasting in blocks of text.
Can anyone offer some advice?
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
stringvalue = Target.Value
statuschange = 0
Application.EnableEvents = False
'Check for text only cells
If Target.Column = 19 Then
stringvalue = UCase(stringvalue)
chars = Len(stringvalue)
'newvalue = ""
For Counter = 1 To chars
Select Case Counter
Case 1, 2, 9
If Asc(Mid(stringvalue, Counter, 1)) >= 65 And Asc(Mid(stringvalue, Counter, 1)) <= 90 Then
Else
statuschange = 1
End If
Case 3 To 8
If Asc(Mid(stringvalue, Counter, 1)) >= 48 And Asc(Mid(stringvalue, Counter, 1)) <= 57 Then
Else
statuschange = 1
End If
End Select
Next Counter
End If
Application.EnableEvents = True
'Error messages
If statuschange = 1 Then
mymessage = MsgBox("The National Insurance number you have entered is invalid. You have an invalid format for an NI number." & vbNewLine & "Please ensure you enter a National Insurance number using the format AA123456A", vbOKOnly, "INVALID ENTRIES - NATIONAL INSURANCE NUMBER")
ElseIf statuschange = 2 Then
mymessage = MsgBox("The National Insurance number you have entered is invalid. You have the wrong number of numerical characters." & vbNewLine & "Please ensure you enter a National Insurance number using the format AA123456A", vbOKOnly, "INVALID ENTRIES - NATIONAL INSURANCE NUMBER")
End If
Target.Select
End Sub