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"},""))))

Sample DataHouse #Address
123 Main Street123Main Street
7 Jones Blvd7Jones Blvd
Super StSuper St
Market Road Flat 2Market Road Flat 2
4567 Elm St #74567Elm St #7

<tbody>
</tbody>


Thank you for any help.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
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
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

<tbody>
</tbody>
Sheet1

Worksheet Formulas
CellFormula
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)

<tbody>
</tbody>

<tbody>
</tbody>

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
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

<colgroup><col style="width: 25pxpx"><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet672

Worksheet Formulas
CellFormula
B2=IF(ISNUMBER(LEFT(A2)+0),LEFT(A2,FIND(" ",A2))+0,"")
C2=TRIM(SUBSTITUTE(A2,B2,"",1))

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>



Formulas copied down.

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

Forum statistics

Threads
1,214,655
Messages
6,120,760
Members
448,991
Latest member
Hanakoro

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