[Calc][Basic] PLATEISSUED and PLATEOFFICE: read the age of a UK number plate in a cell

Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by OpenOffice.org [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This section is not for asking questions about writing your own macros.
Post Reply
User avatar
CarCheckHQ
Banned
Posts: 1
Joined: Wed Nov 25, 2026 2:16 pm

[Calc][Basic] PLATEISSUED and PLATEOFFICE: read the age of a UK number plate in a cell

Post by CarCheckHQ »

Two small Basic functions for Calc that read a UK number plate (current GB format, September 2001 onwards). One tells you when it was issued, the other which DVLA office the first two letters belong to. I use them on a list of used car adverts so I can sort by plate age without doing the maths in my head.

Paste the module into My Macros > Standard (Tools > Macros > Organize Macros > Basic), then in any cell:

Code: Select all

A2: KM19 TXA
=PLATEISSUED(A2)   ->  March 2019 to August 2019
=PLATEOFFICE(A2)   ->  Northampton

A3: for sale: ford fiesta wk69abc, one owner
=PLATEISSUED(A3)   ->  September 2019 to February 2020
=PLATEOFFICE(A3)   ->  Truro
How it works, if you want to adapt it:
  • The two digits are the age identifier. From 1 March they're the year (19 = March to August 2019). From 1 September they're the year plus 50 (69 = September 2019 to February 2020). 00, 01 and 50 were never issued, so those come back as "has not been issued".
  • The first letter of the memory tag is the region and the second picks the office, so LV is Sidcup and SN is Edinburgh. The table follows DVLA leaflet INF104.
  • It finds the first plate anywhere in the cell text, with or without the space, in upper or lower case. Older prefix and suffix plates and Northern Ireland plates aren't handled.
  • LATEST_AGE at the top is the newest age identifier issued (76 at the moment). Bump it each March and September or new plates will show as not issued.
  • Basic doesn't short-circuit And/Or, so the plate scan pads the text with spaces rather than guarding Mid() with a position check. That bit caught me out the first time.
Run PlateSelfTest from Tools > Macros > Run Macro and it checks the tables (18 checks, all pass for me on LibreOffice 25.2; nothing in it is LibreOffice-only, so it should be the same on AOO 4.1).

One thing worth knowing if you use this to check adverts: a private plate can make a car look older than it is, never newer. So the plate gives you the newest the car could be, not its real age.

Disclosure: I'm with CarCheckHQ, a small UK car check site. The macro is CC0, nothing to buy or sign up for. If you only need to check one plate, there's a browser version too: UK number plate decoder.

Code: Select all

REM  PlateAge: Calc functions for UK number plates (current GB format, Sept 2001 on)
REM  =PLATEISSUED(A2)  ->  "March 2019 to August 2019"
REM  =PLATEOFFICE(A2)  ->  "Northampton"
REM  Tables follow DVLA leaflet INF104. CC0, use it however you like.
REM  LATEST_AGE is the newest age identifier issued (76 = September 2026).
REM  Bump it each March and September.

Const LATEST_AGE = 76

REM Finds the first AB12 CDE style plate in the text and returns "AB12" or ""
REM Accepts "KM19 TXA", "km19txa" or a plate buried in an advert title
Private Function PlateFront(ByVal sText As String) As String
	Dim s As String, i As Long, p As Long, sTag As String, sAge As String
	s = " " & UCase(sText) & "  "
	PlateFront = ""
	For i = 2 To Len(s) - 7
		If Not IsLetters(Mid(s, i - 1, 1)) And IsLetters(Mid(s, i, 2)) Then
			sTag = Mid(s, i, 2)
			p = i + 2
			If Mid(s, p, 1) = " " Then p = p + 1
			If IsDigits(Mid(s, p, 2)) Then
				sAge = Mid(s, p, 2)
				p = p + 2
				If Mid(s, p, 1) = " " Then p = p + 1
				If IsLetters(Mid(s, p, 3)) And Not IsLetters(Mid(s, p + 3, 1)) Then
					PlateFront = sTag & sAge
					Exit Function
				End If
			End If
		End If
	Next i
End Function

Private Function IsLetters(s As String) As Boolean
	Dim i As Long
	IsLetters = (Len(s) > 0)
	For i = 1 To Len(s)
		If Mid(s, i, 1) < "A" Or Mid(s, i, 1) > "Z" Then IsLetters = False
	Next i
End Function

Private Function IsDigits(s As String) As Boolean
	Dim i As Long
	IsDigits = (Len(s) > 0)
	For i = 1 To Len(s)
		If Mid(s, i, 1) < "0" Or Mid(s, i, 1) > "9" Then IsDigits = False
	Next i
End Function

REM Two-digit age identifier -> issue window, or "" if never issued
Function AgeWindow(ByVal nAge As Integer) As String
	Dim nLastMarch As Integer, y As Integer
	If LATEST_AGE >= 51 Then nLastMarch = LATEST_AGE - 50 Else nLastMarch = LATEST_AGE
	AgeWindow = ""
	If nAge > LATEST_AGE Or nAge < 2 Or nAge = 50 Then Exit Function
	If nAge > nLastMarch And nAge < 51 Then Exit Function
	If nAge >= 51 Then
		y = 2000 + nAge - 50
		AgeWindow = "September " & y & " to February " & (y + 1)
	Else
		y = 2000 + nAge
		AgeWindow = "March " & y & " to August " & y
	End If
End Function

REM First two letters -> DVLA office, or "" if not a regional tag
Function TagOffice(ByVal sTag As String) As String
	Dim t As Variant, i As Long, j As Long, parts As Variant, kv As Variant
	t = Array( _
		"A:Peterborough=ABCDEFGHJKLMN;Norwich=OPRSTU;Ipswich=VWXY", _
		"B:Birmingham=ABCDEFGHJKLMNOPRSTUVWXY", _
		"C:Cardiff=ABCDEFGHJKLMNO;Swansea=PRSTUV;Bangor=WXY", _
		"D:Chester=ABCDEFGHJK;Shrewsbury=LMNOPRSTUVWXY", _
		"E:Chelmsford=ABCDEFGHJKLMNOPRSTUVWXY", _
		"F:Nottingham=ABCDEFGHJKLMNP;Lincoln=RSTVWXY", _
		"G:Maidstone=ABCDEFGHJKLMNO;Brighton=PRSTUVWXY", _
		"H:Bournemouth=ABCDEFGHJ;Portsmouth=KLMNOPRSTUVWXY", _
		"K:Borehamwood=ABCDEFGHJKL;Northampton=MNOPRSTUVWXY", _
		"L:Wimbledon=ABCDEFGHJ;Borehamwood=KLMNOPRST;Sidcup=UVWXY", _
		"M:Manchester=ABCDEFGHJKLMOPRSTUVWXY", _
		"N:Newcastle=ABCDEGHJKLMNO;Stockton=PRSTUVWXY", _
		"O:Oxford=ABCDEFGHJKLMNOPRSTUVWXY", _
		"P:Preston=ABCDEFGHJKLMNOPRST;Carlisle=UVWXY", _
		"R:Theale=ABCDEFGHJKLMNOPRSTUVWXY", _
		"S:Glasgow=ABCDEFGHJ;Edinburgh=KLMNO;Dundee=PRST;Aberdeen=UVW;Inverness=XY", _
		"V:Worcester=ABCDEFGHJKLMNOPRSTUVWXY", _
		"W:Exeter=ABCDEFGHJ;Truro=KL;Bristol=MNOPRSTUVWXY", _
		"Y:Leeds=ABCDEFGHJKL;Sheffield=MNOPRSTV;Beverley=UWXY")
	TagOffice = ""
	sTag = UCase(sTag)
	For i = LBound(t) To UBound(t)
		If Left(t(i), 1) = Left(sTag, 1) Then
			parts = Split(Mid(t(i), 3), ";")
			For j = LBound(parts) To UBound(parts)
				kv = Split(parts(j), "=")
				If InStr(1, kv(1), Mid(sTag, 2, 1), 0) > 0 Then
					TagOffice = kv(0)
					Exit Function
				End If
			Next j
		End If
	Next i
End Function

REM Cell function: when was this plate issued?
Function PLATEISSUED(ByVal sPlate As Variant) As String
	Dim f As String, w As String
	f = PlateFront(CStr(sPlate))
	If f = "" Then
		PLATEISSUED = "no current-format plate"
		Exit Function
	End If
	w = AgeWindow(CInt(Mid(f, 3, 2)))
	If w = "" Then w = Mid(f, 3, 2) & " has not been issued"
	PLATEISSUED = w
End Function

REM Cell function: which DVLA office do the first two letters belong to?
Function PLATEOFFICE(ByVal sPlate As Variant) As String
	Dim f As String, o As String
	f = PlateFront(CStr(sPlate))
	If f = "" Then
		PLATEOFFICE = ""
		Exit Function
	End If
	o = TagOffice(Left(f, 2))
	If o = "" Then o = "not a regional tag"
	PLATEOFFICE = o
End Function

REM Run this from Tools > Macros to check the tables
Sub PlateSelfTest
	Dim c As Variant, i As Long, nFail As Long, sOut As String
	c = Array( _
		Array(AgeWindow(51), "September 2001 to February 2002"), _
		Array(AgeWindow(2), "March 2002 to August 2002"), _
		Array(AgeWindow(26), "March 2026 to August 2026"), _
		Array(AgeWindow(76), "September 2026 to February 2027"), _
		Array(AgeWindow(1), ""), Array(AgeWindow(50), ""), _
		Array(AgeWindow(27), ""), Array(AgeWindow(77), ""), _
		Array(TagOffice("AB"), "Peterborough"), Array(TagOffice("LV"), "Sidcup"), _
		Array(TagOffice("SN"), "Edinburgh"), Array(TagOffice("YU"), "Beverley"), _
		Array(TagOffice("ZZ"), ""), _
		Array(PLATEISSUED("for sale: ford fiesta km19txa, one owner"), "March 2019 to August 2019"), _
		Array(PLATEOFFICE("KM19 TXA"), "Northampton"), _
		Array(PLATEISSUED("WK69 ABC"), "September 2019 to February 2020"), _
		Array(PLATEOFFICE("for sale: ford fiesta wk69abc, one owner"), "Truro"), _
		Array(PLATEISSUED("AB29 CDE"), "29 has not been issued"))
	For i = LBound(c) To UBound(c)
		If c(i)(0) <> c(i)(1) Then
			nFail = nFail + 1
			sOut = sOut & "FAIL " & i & ": got [" & c(i)(0) & "] want [" & c(i)(1) & "]" & Chr(10)
		End If
	Next i
	If nFail = 0 Then sOut = "PlateSelfTest: all " & (UBound(c) + 1) & " checks passed"
	MsgBox sOut
End Sub
User avatar
MrProgrammer
Moderator
Posts: 5480
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: [Calc][Basic] PLATEISSUED and PLATEOFFICE: read the age of a UK number plate in a cell

Post by MrProgrammer »

The undocumented Like operator is a simple way to test for numbers or letters. I learned about it from topics in the forum.

Private Function IsLetters(s As String) As Boolean
	IsLetters = Not(s Like "*[!A-Z]*")
End Function

Private Function IsDigits(s As String) As Boolean
	IsDigits = Not(s Like "*[!0-9]*")
End Function

s Like "*[!A-Z]*"    tests whether s contains a non-letter character [!A-Z]. If it doesn't, then s contains only letters.
s Like "*[!0-9]*"    tests whether s contains a non-digit character, [!0-9]. If it doesn't, then s contains only digits.

Both functions above return True for the empty string, because it doesn't contain any non-letter or non-digit characters. If it is important to return False for the empty string add an additional test.

Private Function IsLetters(s As String) As Boolean
	If Len(s)=0 Then IsLetters = False : Exit Function
	IsLetters = Not(s Like "*[!A-Z]*")
End Function

Private Function IsDigits(s As String) As Boolean
	If Len(s)=0 Then IsDigits = False : Exit Function
	IsDigits = Not(s Like "*[!0-9]*")
End Function
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.7.8, iMac Intel.   The locale for any menus or Calc formulas in my posts is English (USA).
Post Reply