Formatting UK National Insurance Numbers

X

XL999

I want to be able to type in UK NI numbers as say EG123456XZ and have
them formatted in a cell so that they appear as EG 12 34 56 XZ

Can anyone help with a simple formatting to do this?
 
S

Sandy Mann

The XZ makes the *number* text and you cannot format text like you can
numbers. You could have an event macro run and change the cell entry.
Right-click on the sheet tab and enter this code in the Sheet Module:

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim x As Integer
Dim NewValue As String

If Intersect(Target, Range("D:D")) Is Nothing Then Exit Sub
For x = 1 To Len(Target.Value) Step 2
NewValue = NewValue + Mid(Target.Value, x, 2) + " "
Next x

NewValue = Left(NewValue, Len(NewValue) - 1)

Application.EnableEvents = False
Target.Value = NewValue
Application.EnableEvents = True

End Sub

--
HTH

Sandy
In Perth, the ancient capital of Scotland
and the crowning place of kings

(e-mail address removed)
Replace @mailinator.com with @tiscali.co.uk
 
D

Dave Peterson

One way is to use another cell with a formula that will format it the way you
like:

=left(a1,2)&" "&mid(a1,3,2)&" "&mid(a1,5,2)&" "&right(a1,2)
 
R

Rick Rothstein \(MVP - VB\)

The VBA Format function allow for more compact coding....

Private Sub Worksheet_Change(ByVal Target As Range)
If InStr(Target.Value, " ") = 0 And Not _
Intersect(Target, Range("D:D")) Is Nothing Then
Application.EnableEvents = False
Target.Value = Format$(Target.Value, "@@ @@ @@ @@ ")
Application.EnableEvents = True
End If
End Sub

Rick
 
R

Rick Rothstein \(MVP - VB\)

As a matter of fact, in thinking about it, we can remove the EnableEvents
calls since I filter on an included blank space, the Change event won't run
anything on the second call back to it...

Private Sub Worksheet_Change(ByVal Target As Range)
If InStr(Target.Value, " ") = 0 And Not _
Intersect(Target, Range("D:D")) Is Nothing Then
Target.Value = Format$(Target.Value, "@@ @@ @@ @@ ")
End If
End Sub

Rick
 
R

Roger Govier

Hi

Using another column, and assuming your data to be in column A, then
=LEFT(A1,2)&" "&TEXT(MID(A1,3,6),"00 00 00")&" "&RIGHT(A1,LEN(A1)-8)

I am not aware of NI numbers having 2 alpha characters at the end - mine and
my wife's certainly don't, they have a single alpha character.
AA nn nn nn A

The above formula will deal with either case.
Copy down as far as required.
 
R

Rick Rothstein \(MVP - VB\)

Using another column, and assuming your data to be in column A, then
=LEFT(A1,2)&" "&TEXT(MID(A1,3,6),"00 00 00")&" "&RIGHT(A1,LEN(A1)-8)

We can shave off some 14 characters plus a function call by moving those
external spaces inside the TEXT function pattern string and using the MID
function in place of RIGHT function (assuming the entries are "near-normal"
looking, that is)...

=LEFT(J1,2)&TEXT(MID(J1,3,6)," 00 00 00 ")&MID(J1,9,9)

Rick
 
P

Paul Hyett

In microsoft.public.excel on Fri, 28 Dec 2007, Roger Govier
Hi

Using another column, and assuming your data to be in column A, then
=LEFT(A1,2)&" "&TEXT(MID(A1,3,6),"00 00 00")&" "&RIGHT(A1,LEN(A1)-8)

I am not aware of NI numbers having 2 alpha characters at the end -
mine and my wife's certainly don't, they have a single alpha character.
AA nn nn nn A

And the last character can only be A, B, C or D, IIRC.
 
S

Sandy Mann

Thank you Rick - I learned a bit more. I did not realise that @ in Format >
Cell > Number refers to the whole cell entry but in VBA it refers to only
one character.

Also, is there a reason for the trailing space in the format? It is not
like you to have surplus characters <g>

--
Regards,

Sandy
In Perth, the ancient capital of Scotland
and the crowning place of kings

(e-mail address removed)
Replace @mailinator.com with @tiscali.co.uk
 
R

Rick Rothstein \(MVP - VB\)

The VBA Format function allow for more compact coding....
Thank you Rick - I learned a bit more. I did not realise that @ in Format
only one character.

Yes, there is a big differecnce between how the spreadsheet's FORMAT
function and VBA's Format function perform; and for me, that difference is
annoyingly large. In the VBA world, each @ symbol stands for one characters
from the text in the first argument; other characters appearing in the
pattern string, with a few exceptions, simply get placed in the output
string where shown (in this case, the blank spaces).
Also, is there a reason for the trailing space in the format?
It is not like you to have surplus characters <g>

<g> Nope, I don't like surplus characters... and I haven't include any this
time either. Before I can tell you why the space is not surplus, I have to
explain the way the @ symbols get used in the pattern string.

There are two ways to fill the format pattern string when using the @
symbol... right-to-left and, of course, left-to-right. But, believe it or
not, the two methods do not work the same way when it comes to a string
longer than the number of @ symbols. First, the normal method of filling in
the @ symbols with characters from the first argument is from right to left.
This is usually used to right-justify text in a column. If you start the
pattern string off with an exclamation point (!), then the @ symbols are
filled in from left to right. Now, the difference... if you use the
exclamation point in front of the pattern string, and if your text is longer
than the number @ signs to be filled in, any excess characters are truncated
away and not displayed. However, for the non-exclamation pattern string,
characters fill from the right with each newly added character pushing the
previous character over one @ symbol position to the left to make room for
it in the right-most @ symbol's position. This goes on until the last @
symbol has been filled in. At this point, if the text contains more
characters than @ symbols, they are NOT truncated away... they are simply
concatenated on the end of the pattern string.

Okay, armed with this explanation, notice I did not use an exclamation point
in front of the pattern string AND that I provided a pattern string with
only 8 @ symbols in it even though I know there text string will be more
than 8 characters long. Doing this guarantees the excess characters will be
concatenated after the pattern string. The trailing blank space is the one
used to separate the trailing letters from the last digit in the inputted
text string.

Rick
 
S

Sandy Mann

Thank you for the very full explanation Rick, I have read it through,
(several times), and I see what you are saying but I find the following in
XL97:
filled in from left to right. Now, the difference... if you use the
exclamation point in front of the pattern string, and if your text is
longer than the number @ signs to be filled in, any excess characters are
truncated away and not displayed.

With an entry of 1234567890AB and the VBA Format of "!@@ @@ @@ @@ "

I get 12 34 56 78 90AB

ie the *extra* characters are concatenated at the end not truncated.

Also your If statement:

If InStr(Target.Value, " ") = 0 And Not _
Intersect(Target, Range("D:D")) Is Nothing Then

causes a Run Time Error 13 Type Mismatch whenever a multy-cell range is
actioned including dragging formulas and deleting ranges.

I don't know if it is different in other versions.

--
Regards,


Sandy
In Perth, the ancient capital of Scotland
and the crowning place of kings

(e-mail address removed)
Replace @mailinator.com with @tiscali.co.uk
 
R

Rick Rothstein \(MVP - VB\)

Hmm! I gather you are using XL97. I am using XL2003 and I also have XL2007
installed too... the description I gave for the VBA Format function reflects
what I see in both of my versions of Excel. For example, when I run this
code in the Immediate window inside the VBA editor...

Value = "12345678ABCD"
? "<" & Format(Value,"@@ @@ @@ @@ ") & ">"
? "<" & Format(Value,"!@@ @@ @@ @@ ") & ">"

I get these two results printed out...

1st Statement: 12 34 56 78 ABCD
2nd Statement: 56 78 AB CD

You are getting something different when you execute them? I was under the
impression that XL97 on upwards all used VBA Version 6 for their macro
languages... am I mistaken?

As for the code problem you pointed out, thanks for bringing it to my
attention; I think this fixes it...

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count = 1 Then
If InStr(Target.Value, " ") = 0 And _
Not Intersect(Target, Range("D:D")) Is Nothing Then
Target.Value = Format$(Target.Value, "@@ @@ @@ @@ ")
End If
End If
End Sub

Rick
 
S

Sandy Mann

Hi Rick,

If I run this Macro:

Sub test()
PValue = "12345678ABCD"
Debug.Print "<" & Format(PValue, "@@ @@ @@ @@ ") & ">"
Debug.Print "<" & Format(PValue, "!@@ @@ @@ @@ ") & ">"
End Sub


I get:

<123456 78 AB CD >
<12 34 56 78 ABCD>

Returned in the immediate window so I assume that there is a difference in
the versions.


--
HTH

Sandy
In Perth, the ancient capital of Scotland
and the crowning place of kings

(e-mail address removed)
Replace @mailinator.com with @tiscali.co.uk
 
R

Rick Rothstein \(MVP - VB\)

If I run this Macro:
Sub test()
PValue = "12345678ABCD"
Debug.Print "<" & Format(PValue, "@@ @@ @@ @@ ") & ">"
Debug.Print "<" & Format(PValue, "!@@ @@ @@ @@ ") & ">"
End Sub


I get:

<123456 78 AB CD >
<12 34 56 78 ABCD>

And I get this...

<12 34 56 78 ABCD>
<56 78 AB CD >

Am I wrong in thinking XL97 has VBA Version 6 at its heart? If you do a
Help/About from the VBA editor's menu bar, what version of VB is reported?

Rick
 
S

Sandy Mann

Am I wrong in thinking XL97 has VBA Version 6 at its heart? If you do a
Help/About from the VBA editor's menu bar, what version of VB is reported?

It doesn't say I just get a dialog box reporting:

Microsoft Visual Basic
Copyright (c) 1987 - 1996 Microsoft Corp


--
Regards,

Sandy
In Perth, the ancient capital of Scotland
and the crowning place of kings

(e-mail address removed)
Replace @mailinator.com with @tiscali.co.uk
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top