Extract House Number

samedi 25 avril 2015

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 it will contain just the House name. If there is no house number then I need to show the house name.

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://ift.tt/1QsLlYl original code
'Extract numbers from text
'http://ift.tt/1DvqJWz

'Adapted by
'A house number of "0" (Zero) is not acceped by this function

Dim intLen  As Integer
Dim intCounter As Integer
Dim strNumber As String
Dim blnFoundNumber As Boolean

blnFoundNumber = False
strInString = Trim(strInString) 'Removes leading & trending 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
                    If blnFoundNumber = True Then Exit Do
                    intCounter = intCounter + 1 'It wasn't a number, 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

Extract House Number

0 commentaires:

Enregistrer un commentaire

Labels