- Local time
- Today, 10:52
- Joined
- Jul 9, 2003
- Messages
- 17,339
I needed to extract the house number from the first line of an address for a HMRC GiftAid Claim form.
HMRC requirements are that the field should contain either the house number or the house name, and no other information. The next field should contain the postcode.
My assumption is that the first line of the address will contain either the house number and street name or house name. If the first character(s) are numbers, then that's the house number. If there is/are NO NUMBER(s) then it's the house name. This is for the HMRC Gift Aid Spreadsheet found HERE:- Gift Aid: schedule spreadsheets to claim back tax on donations
I found this AWF thread:- Extract numbers from text
and have adapted it to my needs and this is the result.
I have only tried it on some dummy data, will be interesting to see how it operates on the real data!!!
HMRC requirements are that the field should contain either the house number or the house name, and no other information. The next field should contain the postcode.
My assumption is that the first line of the address will contain either the house number and street name or house name. If the first character(s) are numbers, then that's the house number. If there is/are NO NUMBER(s) then it's the house name. This is for the HMRC Gift Aid Spreadsheet found HERE:- Gift Aid: schedule spreadsheets to claim back tax on donations
I found this AWF thread:- Extract numbers from text
and have adapted it to my needs and this is the result.
I have only tried it on some dummy data, will be interesting to see how it operates on the real data!!!
Code:
Function fGetHouseNumberOrName(ByVal strInString As String) As String
'Found HERE:-
'Adapted from raskew's http://www.access-programmers.co.uk/forums/member.php?u=4714 original code
'Extract numbers from text
'http://www.access-programmers.co.uk/forums/showthread.php?t=219652
Dim intLen As Integer
Dim intCounter As Integer
Dim strNumber As String
Dim blnFoundNumber As Boolean
blnFoundNumber = False
strInString = Trim(strInString) 'Removes leading & trailing spaces
intLen = Len(strInString) 'Stores original length
intCounter = 1 'Counter & position marker
If strInString = "" Or IsNull(strInString) Or intLen = 0 Then Exit Function 'validate we didn't get passed an empty/null string
Do
If IsNumeric(Mid(strInString, intCounter, 1)) Then 'Check if that single character is a number
blnFoundNumber = True
strNumber = strNumber & Mid(strInString, intCounter, 1) 'If it is add to existing ones (if any)
intCounter = intCounter + 1 'Add to counter so we know to go to next character on the next pass/loop
Else
If intCounter = 1 Then Exit Do 'The first Character "is Not Numeric" so don't Check any more, so Exit
If blnFoundNumber = True Then Exit Do 'This Character is "is Not Numeric" so your House number is finished, so Exit
intCounter = intCounter + 1 'It "is Not Numeric", add to counter so we know to skip it
End If
Loop Until intLen = (intCounter - 1) 'Go until we processed all characters. The reason we have to do intCounter -1 is that Len starts at 0 & we told intCounter to start at 1
If IsNumeric(strNumber) Then
fGetHouseNumberOrName = strNumber
Else
fGetHouseNumberOrName = strInString
End If
End Function 'fGetHouseNumberOrName
Last edited: