extract the first characters from a postcode

Swifey

Active Member
Joined
Jan 16, 2003
Messages
421
Is there a formula to extract the fist characters from a postcode?

for example:

AL5 5SH = AL
B22 2SH = B
and so on....

Thanks for your help
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
You can create your own function, something like this:

Code:
Function RemoveChar(myString As String) As String

    Dim i As Long
    Dim myLen As Long
    myLen = Len(myString)
    If myLen > 0 Then
        For i = 1 To myLen
            If IsNumeric(Mid(myString, i, 1)) Then Exit For
        Next i
        RemoveChar = Right(myString, myLen - i + 1)
    End If
                
End Function
 
Upvote 0
Hi I have created and added your code to a module - I then used the expression builder in my query to select the new function but I get an error message stating "Undefined function 'RemoveChar' in expression.

I have never used this method before can you please advise me were I am going wrong.

Many Thanks :oops:
 
Upvote 0
Why not just try this?

FirstPart:Left([Postcode], (Instr([Postcode], " ")-1)
 
Upvote 0
Thanks for your input - Unfortunately its bringing back the numbers. I only want to pull back the first Characters from the post code ie

E1 1JP = E
EE1 1JP = EE
B1 1JP = B
BP11 1JP = BP

and so on..............

Many Thanks
 
Upvote 0
Here is a modification of jmisky code
Code:
Function RemoveChar(myString As String) As String

    Dim i As Long
    Dim myLen As Long
    myLen = Len(myString)
    If myLen > 0 Then
        For i = 1 To myLen
            If IsNumeric(Mid(myString, i, 1)) Then Exit For
        Next i
        RemoveChar = Left(myString, i - 1)
    End If
                
End Function

The error message you got with the orignal try is becasue you saved the module with the same name as the function.
rename the module modRemoveChar or something


HTH

Peter
 
Upvote 0
The error message you got with the orignal try is becasue you saved the module with the same name as the function.

It could also be because the code wasn't compiled especially if using later versions of Access.

To compile the code goto Debug when in module design view and select compile.

I've just spent about 10 minutes trying to get the function to work with Access 2000 and that was what was causing the problem.
 
Upvote 0

Forum statistics

Threads
1,221,805
Messages
6,162,074
Members
451,738
Latest member
gaseremad

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top