Extract House Number from String

DJDJDJDJ

New Member
Joined
Jun 10, 2015
Messages
6
Hi,

I have a column with address details. I would like to separate the house number (always on the left) from the string. In some case, there might not be a house number, while in some other cases there might be a house and a flat number. I am only looking for the house number.

Table below, shows some sample data and the extracted columns I would like to have.

I have tried the following but I am not getting the intended results. Somehow it is pulling some letters as well.

Code:
=LEFT(S2,SUM(LEN(S2)-LEN(SUBSTITUTE(S2,{"0","1","2","3","4","5","6","7","8","9"},""))))

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Sample Data[/TD]
[TD]House #[/TD]
[TD]Address[/TD]
[/TR]
[TR]
[TD] 123 Main Street[/TD]
[TD]123[/TD]
[TD]Main Street[/TD]
[/TR]
[TR]
[TD]7 Jones Blvd[/TD]
[TD]7[/TD]
[TD]Jones Blvd[/TD]
[/TR]
[TR]
[TD]Super St[/TD]
[TD][/TD]
[TD]Super St[/TD]
[/TR]
[TR]
[TD]Market Road Flat 2[/TD]
[TD][/TD]
[TD]Market Road Flat 2[/TD]
[/TR]
[TR]
[TD]4567 Elm St #7[/TD]
[TD]4567[/TD]
[TD]Elm St #7[/TD]
[/TR]
</tbody>[/TABLE]


Thank you for any help.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hey,

the following UDF will extract only numbers from a string, although this may cause problems such as the "4567 Elm St #7 " example as it would return "45677" - but feel free to give it a try if there are not many cases where there are additional numbers laying around.

Code:
Function NUMONLY(txt As String) As String
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "\D"
        NUMONLY = .Replace(txt, "")
    End With
End Function

To use:
=NUMONLY(S2)

Where S2 is your string
 
Upvote 0
How about this

Below formula to get the house number
=IF(ISNUMBER(LEFT(A2,FIND(" ",A2)-1)+0)=TRUE,LEFT(A2,FIND(" ",A2)-1)+0,"")

Below formula to get the address
=IF(ISNUMBER(LEFT(A2,FIND(" ",A2)-1)+0)=TRUE,TRIM(MID(A2,FIND(" ",A2),99)),A2)
 
Upvote 0
Same thing just to make more clear ...


Book1
ABC
1Sample DataHouse #Address
2123 Main Street123Main Street
37 Jones Blvd7Jones Blvd
4Super StSuper St
5Market Road Flat 2Market Road Flat 2
64567 Elm St #74567Elm St #7
Sheet1
Cell Formulas
RangeFormula
B2=IF(ISNUMBER(LEFT(A2,FIND(" ",A2)-1)+0)=TRUE,LEFT(A2,FIND(" ",A2)-1)+0,"")
C2=IF(ISNUMBER(LEFT(A2,FIND(" ",A2)-1)+0)=TRUE,TRIM(MID(A2,FIND(" ",A2),99)),A2)
 
Upvote 0
Hey,

the following UDF will extract only numbers from a string, although this may cause problems such as the "4567 Elm St #7 " example as it would return "45677" - but feel free to give it a try if there are not many cases where there are additional numbers laying around.

Code:
Function NUMONLY(txt As String) As String
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "\D"
        NUMONLY = .Replace(txt, "")
    End With
End Function

To use:
=NUMONLY(S2)

Where S2 is your string



How about this

Below formula to get the house number
=IF(ISNUMBER(LEFT(A2,FIND(" ",A2)-1)+0)=TRUE,LEFT(A2,FIND(" ",A2)-1)+0,"")

Below formula to get the address
=IF(ISNUMBER(LEFT(A2,FIND(" ",A2)-1)+0)=TRUE,TRIM(MID(A2,FIND(" ",A2),99)),A2)

Same thing just to make more clear ...

ABC
123 Main StreetMain Street
7 Jones BlvdJones Blvd
Super StSuper St
Market Road Flat 2Market Road Flat 2
4567 Elm St #7Elm St #7

<tbody>
[TD="align: center"]1[/TD]
[TD="bgcolor: #FFFF00"]Sample Data[/TD]
[TD="bgcolor: #FFFF00"]House #[/TD]
[TD="bgcolor: #FFFF00"]Address[/TD]

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

[TD="align: right"]123[/TD]

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

[TD="align: right"]7[/TD]

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

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

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

[TD="align: right"]4567[/TD]

</tbody>
Sheet1

[TABLE="width: 85%"]
<tbody>[TR]
[TD]Worksheet Formulas[TABLE="width: 100%"]
<tbody>[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]
[TH]Cell[/TH]
[TH="align: left"]Formula[/TH]
[/TR]
[TR]
[TH="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]B2[/TH]
[TD="align: left"]=IF(ISNUMBER(LEFT(A2,FIND(" ",A2)-1)+0)=TRUE,LEFT(A2,FIND(" ",A2)-1)+0,"")[/TD]
[/TR]
[TR]
[TH="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]C2[/TH]
[TD="align: left"]=IF(ISNUMBER(LEFT(A2,FIND(" ",A2)-1)+0)=TRUE,TRIM(MID(A2,FIND(" ",A2),99)),A2)[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]

Thank you guys. You are all awesome. I ended up using the formula instead of the script. Only because I need to learn a bit more about scripting. This script, however gives me a good start.

Much appreciated.

Best.
 
Upvote 0
Upvote 0
Hi,

Also try this:


Book1
ABC
1Sample DataHouse #Address
2123 Main Street123Main Street
37 Jones Blvd7Jones Blvd
4Super StSuper St
5Market Road Flat 2Market Road Flat 2
64567 Elm St #74567Elm St #7
77 Elm St #77Elm St #7
Sheet672
Cell Formulas
RangeFormula
B2=IF(ISNUMBER(LEFT(A2)+0),LEFT(A2,FIND(" ",A2))+0,"")
C2=TRIM(SUBSTITUTE(A2,B2,"",1))


Formulas copied down.
 
Upvote 0
I'd probably look at something like this:
https://www.experts-exchange.com/ar...asic-for-Applications-and-Visual-Basic-6.html

there are some built in patterns such as the one I used "\D" which refers to non-numerical values, then I basically replaced all of the non-numerical values with blank, thus leaving numerical values only. It's pretty cool!

Thank you very much. :-)

Hi,

Also try this:

ABC
Sample DataHouse #Address
123 Main StreetMain Street
7 Jones BlvdJones Blvd
Super StSuper St
Market Road Flat 2Market Road Flat 2
4567 Elm St #7Elm St #7
7 Elm St #7Elm St #7

<colgroup><col style="width: 25pxpx"><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]

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

[TD="align: right"]123[/TD]

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

[TD="align: right"]7[/TD]

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

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

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

[TD="align: right"]4567[/TD]

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

[TD="align: right"]7[/TD]

</tbody>
Sheet672

[TABLE="width: 85%"]
<tbody>[TR]
[TD]Worksheet Formulas[TABLE="width: 100%"]
<thead>[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]
[TH="width: 10px"]Cell[/TH]
[TH="align: left"]Formula[/TH]
[/TR]
</thead><tbody>[TR]
[TH="width: 10px, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]B2[/TH]
[TD="align: left"]=IF(ISNUMBER(LEFT(A2)+0),LEFT(A2,FIND(" ",A2))+0,"")[/TD]
[/TR]
[TR]
[TH="width: 10px, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]C2[/TH]
[TD="align: left"]=TRIM(SUBSTITUTE(A2,B2,"",1))[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]



Formulas copied down.

Thank you.
 
Upvote 0
You're welcome, thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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