Removing characters with VBA

paulsolar

Well-known Member
Joined
Aug 21, 2013
Messages
696
Office Version
  1. 365
Hi All

I thought this was going to be easy to accomplish but I'm having a lot of trouble with it.

I have a long list of postcodes, in the UK postcodes come in quite a few formats but i just want to achieve a simple thing.

Postcodes can look like the below:

NP12 9AF
NP126AF
CF32 9PP
B1 9AA

I only need the letters before the first number, so my new list would look like the below

NP
NP
CF
B

I'm sure there must be an easy way of doing this but cant seem to come up with a simple solution

Any help is greatly appreciated.

Cheers

Paul
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hi Gas Man

many thaks for your reply

sorry to say im still struggling with this, I'll keep you posted as to how i get on

cheers

Paul
 
Upvote 0
How about
Code:
Sub Postcodes2()

   Dim Ary As Variant
   Dim Rw As Long
   
   Ary = Range("A2", Range("A" & Rows.Count).End(xlUp))
   
   For Rw = LBound(Ary, 1) To UBound(Ary, 1)
      If IsNumeric(Mid(Ary(Rw, 1), 2, 1)) Then
         Ary(Rw, 1) = Left(Ary(Rw, 1), 1)
      Else
         Ary(Rw, 1) = Left(Ary(Rw, 1), 2)
      End If
   Next Rw
   Range("B2").Resize(UBound(Ary, 1)) = Ary

End Sub
This assumes the postcodes are in Col A & will put the result in col B
 
Upvote 0
If you just want a formula:

Excel 2012
AB
NP12 9AFNP
NP126AFNP
CF32 9PPCF
B1 9AAB

<tbody>
[TD="align: center"]1[/TD]

[TD="align: center"]2[/TD]

[TD="align: center"]3[/TD]

[TD="align: center"]4[/TD]

</tbody>
Sheet3

[TABLE="width: 85%"]
<tbody>[TR]
[TD]Worksheet Formulas[TABLE="width: 100%"]
<tbody>[TR]
[TH]Cell[/TH]
[TH="align: left"]Formula[/TH]
[/TR]
[TR]
[TH]B1[/TH]
[TD="align: left"]=LEFT(A1,MIN(IF(ISNUMBER(MID(A1&1,{1,2,3,4,5,6,7,8,9},1)+0),{0,1,2,3,4,5,6,7,8})))
[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]
 
Last edited:
Upvote 0
Hi Fluff

twice in two days you've saved my life, absolutely fantastic.

I can go to work tomorrow without having to panic :)
Cheers

Paul
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0
Fomula Answere
A3=Data
ARRAY Formula in B3
Code:
=IFERROR(LEFT(A3,SMALL(IF(ISNUMBER(FIND(MID(A3,ROW(INDIRECT("1:"&LEN(A3))),1),"123456789")),ROW(INDIRECT("1:"&LEN(A3))),""),1)-1),"")
ARRAY formula is used


To enter ARRAY formula
Paste the formula
Press F2
Press Ctrl+Shift+Enter keys together.
formula will be covered with{} brackets by excel.
 
Last edited:
Upvote 0
Hi Eric

I was going down the formula route until Fluff came up with the superb bit of code.

Albeit my formula was about ten times longer than yours :eeek:

thanks for your help Guys really appreciate it.

cheers

Paul
 
Upvote 0
How about
Code:
Sub Postcodes2()

   Dim Ary As Variant
   Dim Rw As Long
   
   Ary = Range("A2", Range("A" & Rows.Count).End(xlUp))
   
   For Rw = LBound(Ary, 1) To UBound(Ary, 1)
      If IsNumeric(Mid(Ary(Rw, 1), 2, 1)) Then
         Ary(Rw, 1) = Left(Ary(Rw, 1), 1)
      Else
         Ary(Rw, 1) = Left(Ary(Rw, 1), 2)
      End If
   Next Rw
   Range("B2").Resize(UBound(Ary, 1)) = Ary

End Sub
This assumes the postcodes are in Col A & will put the result in col B

You have so many lines of code. :lol:
Code:
[table="width: 500"]
[tr]
	[td]Sub PostCodes()
  Dim LastRow As Long
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  Range("B1:B" & LastRow) = Evaluate(Replace("IF(A1:A#="""","""",LEFT(A1:A#,2-(ISNUMBER(-MID(A1:A#,2,1)))))", "#", LastRow))
End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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