Break of an address in a cell

Status
Not open for further replies.

drop05

Active Member
Joined
Mar 23, 2021
Messages
285
Office Version
  1. 365
Platform
  1. Windows
I’m VBA. Is this possible, If a cell has an address, state, city, and zip code and state in one cell. Is it possible to break that up into separate cells?
So the address is in
cell A1 = 123 Address st, Dallas, Tx, 12345

and I want to copy the address and break it into different cells and paste into sheet2

so sheet 2
A1 is the address: 123 address st
A2 is city: Dallas
A3 is state: Tx
A4 is zip: 12345
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
You could do text to columns rather than VBA.

There might be a way to insert the array contents straight into a series of columns without the loop.

Code:
Sub SplitAddr()
' split address in A1
Dim addr() As String, i As Long
addr = Split(Range("A1"), ",")
 For i = 0 To UBound(addr)
  Cells(1, i + 2) = addr(i)
 Next i
End Sub
 
Upvote 0
PQ solution

oAAQqn69R4ikZ1sZ6xst_work.csv
ABC
1AddressStreat
2123 Address st, Dallas, Tx, 12345123 Address st
Sheet4


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table4"]}[Content],
    #"Split Column by Delimiter" = Table.SplitColumn(Source, "Address", Splitter.SplitTextByDelimiter(", ", QuoteStyle.Csv), {"Address.1", "Address.2", "Address.3", "Address.4"}),
    #"Renamed Columns" = Table.RenameColumns(#"Split Column by Delimiter",{{"Address.1", "Streat"}, {"Address.2", "City"}, {"Address.3", "State"}, {"Address.4", "Postal code"}})
in
    #"Renamed Columns"

Also @MrExcel in one of his video shows VBA udf function TextSpliter. however i cant find the video now.
 
Upvote 0
Here's another VBA way without a loop:

Code:
Sub SplitAddr()
' split address in A1
Dim addr() As String
addr = Split(Range("A1"), ",")
Range("B1:E1") = addr
End Sub
 
Last edited:
Upvote 0
You could do text to columns rather than VBA.

There might be a way to insert the array contents straight into a series of columns without the loop.

Code:
Sub SplitAddr()
' split address in A1
Dim addr() As String, i As Long
addr = Split(Range("A1"), ",")
 For i = 0 To UBound(addr)
  Cells(1, i + 2) = addr(i)
 Next i
End Sub
I’d like to try the text to column but I am working with two workbooks so not sure if I could use that. Have vba code that transfer the values from a sheet in workbook1 to workbook 2 but some are addresses so thinking of a way to separate

so this code if I’m reading it right. It goes to A1 and splits it until a comma comes across in that cell
Do you mind explaining the for loop a little confused there.
 
Upvote 0
PQ solution

oAAQqn69R4ikZ1sZ6xst_work.csv
ABC
1AddressStreat
2123 Address st, Dallas, Tx, 12345123 Address st
Sheet4


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table4"]}[Content],
    #"Split Column by Delimiter" = Table.SplitColumn(Source, "Address", Splitter.SplitTextByDelimiter(", ", QuoteStyle.Csv), {"Address.1", "Address.2", "Address.3", "Address.4"}),
    #"Renamed Columns" = Table.RenameColumns(#"Split Column by Delimiter",{{"Address.1", "Streat"}, {"Address.2", "City"}, {"Address.3", "State"}, {"Address.4", "Postal code"}})
in
    #"Renamed Columns"

Also @MrExcel in one of his video shows VBA udf function TextSpliter. however i cant find the video now.
Man I need to look into learning power quarry that is awesome. No worries but good to know I’ll try to find it
Mainly working with vba because I’m not too familiar with PQ and everything I wrote is in vba
 
Upvote 0
@drop05
This would be good for PQ since ctrl+alt+f5 refreshes all querries and all the code is re-applied to querry.
you dont have run it every time separetly, but use simple refresh.
I gave you the code from advanced editor, otherwise all the stuff i did i simply clicked my way from the ribbon.
So did not "code it" PQ did it for me.
 
Upvote 0
My last edited post splits it without a loop. The split is done based on a comma, then the array is spread across the four columns.

Code:
Sub SplitAddr()
' split address in A1
Dim addr() As String
addr = Split(Range("A1"), ",")
Range("B1:E1") = addr
End Sub

All in 1 line:

Code:
Sub SplitAddr()
Range("B1:E1") = Split(Range("A1"), ",")
End Sub
 
Upvote 0
@drop05
This would be good for PQ since ctrl+alt+f5 refreshes all querries and all the code is re-applied to querry.
you dont have run it every time separetly, but use simple refresh.
Do you have any recommendations on good videos or websites to look at for learning PQ?
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

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