mykal66
08-03-2012, 11:37 PM
Hi guys i need help validating a UK National Insurance Number in a cell range G2:G200. Format must be LLNNNNNNL (letter case isn't too important but would prefer upper case)
I need any incorrect entry to be highlighted in and different font colour to draw users attention to the incorrect format or a message to advise user of same issue.
I would prefer to use VBA (but have tried conditional formatting too which i also couldn't get to work )and found some code that i cannot get to work as below - can anyone help with the code or have an alternative option
Public Function IsNatInsurNum(DataInput As Variant) As Boolean
Dim mydata As String, Temp As Long
IsNatInsurNum = False
mydata = CStr(DataInput) 'convert to string
If Len(mydata) = 9 Then 'nine characters
If (Asc(Left(mydata, 1)) >= 65 And Asc(Left(mydata, 1)) <= 90)
Then 'first char is A to Z
If (Asc(Mid(mydata, 2, 1)) >= 65 And Asc(Mid(mydata, 2, 1)) <=
90) Then 'second char is A to Z
On Error Resume Next
Err.Clear
Temp = CDbl(Mid(mydata, 3, 6)) 'check 6 digits in middle
If Temp <> 0 Then
If (Asc(Right(mydata, 1)) >= 65 And
Asc(Right(mydata, 1)) <= 90) Then 'last char is A to Z
IsNatInsurNum = True
End If
I need any incorrect entry to be highlighted in and different font colour to draw users attention to the incorrect format or a message to advise user of same issue.
I would prefer to use VBA (but have tried conditional formatting too which i also couldn't get to work )and found some code that i cannot get to work as below - can anyone help with the code or have an alternative option
Public Function IsNatInsurNum(DataInput As Variant) As Boolean
Dim mydata As String, Temp As Long
IsNatInsurNum = False
mydata = CStr(DataInput) 'convert to string
If Len(mydata) = 9 Then 'nine characters
If (Asc(Left(mydata, 1)) >= 65 And Asc(Left(mydata, 1)) <= 90)
Then 'first char is A to Z
If (Asc(Mid(mydata, 2, 1)) >= 65 And Asc(Mid(mydata, 2, 1)) <=
90) Then 'second char is A to Z
On Error Resume Next
Err.Clear
Temp = CDbl(Mid(mydata, 3, 6)) 'check 6 digits in middle
If Temp <> 0 Then
If (Asc(Right(mydata, 1)) >= 65 And
Asc(Right(mydata, 1)) <= 90) Then 'last char is A to Z
IsNatInsurNum = True
End If