Need to convert/transpose single column of addresses to separate columns (name, city, state, etc.), but not all addresses have the same number of rows

Myhobohemia

Board Regular
Joined
Mar 12, 2020
Messages
54
Office Version
  1. 365
Platform
  1. Windows
HI there. As I said in the subject line, I need to convert a single column of addresses and transpose them into their separate fields. This would be fairly easy, except that the number of rows is irregular, ranging from 6-13 rows. Most are about 7 or 8. I can't figure out how to automate this. Right now, the individual addresses in the column are not separated by anything. The last row of each address is a hyperlink, for what it's worth (and hyperlinks are searchable using the search feature, I learned yesterday, but to no avail). I have never done a macro or used VBA in my life! :oops: I can, however, follow clear instructions. ? I have dozens of separate documents, some with hundreds of addresses, some with only a handful. The worstcase scenario would be to have to transpose each address manually. But I'd like to avoid it, particularly since it looks like there will still be plenty of manual work later, getting everything into the right column. Any suggestions?! Thanks in advance. Attaching an image of the data.
 

Attachments

  • 20.03.12  Address data that needs to be transposed.JPG
    20.03.12 Address data that needs to be transposed.JPG
    54.2 KB · Views: 49

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Hello! Sorry for delay in responding. It's great! Worked like a charm. :biggrin: Thank you!
You're welcome & thanks for the feedback.

I've no idea how to get the Zip codes, so cannot help on that.
 
Upvote 0
Here is a way to do it. I just went to USPS.com to lookup the zip code from the address and used that info in a http get request and parsed out the result with some regex.

Book1
ABCDEFGHIJ
1OrderPractice NameStreet AddressAddress line 2CityStateZip CodeWeb linkAccred1Accred1 Date
21Selma Family Care, PC203 Vaughan Memorial DriveSelmaAL36701Selma Family Care, PCNuclear/PET: Myocardial Perfusion Imaging30-Apr-21
32Charlton Trinidad, M.D.1739 East Main StreetSuite AbPrattvilleAL36066Charlton Trinidad, M.D.Nuclear/PET: Myocardial Perfusion Imaging30-Apr-21
43MEDICAL CARE ASSOCIATES PRATTVILLE102 MEDICAL CENTER DR SUITE APrattvilleAL36066MEDICAL CARE ASSOCIATES PRATTVILLENuclear/PET: Myocardial Perfusion Imaging30-Apr-21
54Primary Care Internists1722 Pine StreetMontgomeryAL36106Primary Care InternistsNuclear/PET: Myocardial Perfusion Imaging30-Apr-21
65John K. L. Porter, D.O., PC1758 Park Pl Suite 100MontgomeryAL36106John K. L. Porter, D.O., PCNuclear/PET: Myocardial Perfusion Imaging30-Apr-21
76William O. Sargeant, PC1758 Park Pl Suite 100MontgomeryAL36106William O. Sargeant, PCNuclear/PET: Myocardial Perfusion Imaging30-Apr-21
87Mulberry Medical Associates1301 Mulberry StreetMontgomeryAL36106Mulberry Medical AssociatesNuclear/PET: Myocardial Perfusion Imaging30-Apr-21
Sheet1
Cell Formulas
RangeFormula
G2:G8G2=getZip([@[Street Address]],[@City],[@State])


VBA Code:
Option Explicit

Function getZip(street As String, city As String, state As String) As String
street = Replace(street, " ", "+")
Dim url As String:                  url = "https://m.usps.com/m/QuickZipAction?mode=0&tAddress=" & street & "&tApt=&tCity=" & city & "&sState=" & state & "&jsonInd=Y"
Dim result As String:               result = GetData(url)

With CreateObject("VBScript.RegExp")
    .Pattern = """zip""\s:\s""(\d{5})"
    getZip = .Execute(result)(0).submatches(0)
End With
End Function

Function GetData(myUrl As String) As String
    Dim winHttpReq As Object

    Set winHttpReq = CreateObject("Microsoft.XMLHTTP")
    winHttpReq.Open "GET", myUrl, False
    winHttpReq.Send
    GetData = winHttpReq.ResponseText
End Function
 
Upvote 0
And just for fun, you can do the same thing with Power Query as well.

Book1
ABCDEFGHIJ
1OrderPractice NameStreet AddressAddress line 2CityStateWeb linkAccred1Accred1 Date
21Selma Family Care, PC203 Vaughan Memorial DriveSelmaALSelma Family Care, PCNuclear/PET: Myocardial Perfusion Imaging4/30/2021
32Charlton Trinidad, M.D.1739 East Main StreetSuite AbPrattvilleALCharlton Trinidad, M.D.Nuclear/PET: Myocardial Perfusion Imaging4/30/2021
43MEDICAL CARE ASSOCIATES PRATTVILLE102 MEDICAL CENTER DR SUITE APrattvilleALMEDICAL CARE ASSOCIATES PRATTVILLENuclear/PET: Myocardial Perfusion Imaging4/30/2021
54Primary Care Internists1722 Pine StreetMontgomeryALPrimary Care InternistsNuclear/PET: Myocardial Perfusion Imaging4/30/2021
65John K. L. Porter, D.O., PC1758 Park Pl Suite 100MontgomeryALJohn K. L. Porter, D.O., PCNuclear/PET: Myocardial Perfusion Imaging4/30/2021
76William O. Sargeant, PC1758 Park Pl Suite 100MontgomeryALWilliam O. Sargeant, PCNuclear/PET: Myocardial Perfusion Imaging4/30/2021
87Mulberry Medical Associates1301 Mulberry StreetMontgomeryALMulberry Medical AssociatesNuclear/PET: Myocardial Perfusion Imaging4/30/2021
9
10
11OrderPractice NameStreet AddressAddress line 2CityStateZipWeb linkAccred1Accred1 Date
121Selma Family Care, PC203 Vaughan Memorial DriveSelmaAL36701Selma Family Care, PCNuclear/PET: Myocardial Perfusion Imaging4/30/2021
132Charlton Trinidad, M.D.1739 East Main StreetSuite AbPrattvilleAL36066Charlton Trinidad, M.D.Nuclear/PET: Myocardial Perfusion Imaging4/30/2021
143MEDICAL CARE ASSOCIATES PRATTVILLE102 MEDICAL CENTER DR SUITE APrattvilleAL36066MEDICAL CARE ASSOCIATES PRATTVILLENuclear/PET: Myocardial Perfusion Imaging4/30/2021
154Primary Care Internists1722 Pine StreetMontgomeryAL36106Primary Care InternistsNuclear/PET: Myocardial Perfusion Imaging4/30/2021
165John K. L. Porter, D.O., PC1758 Park Pl Suite 100MontgomeryAL36106John K. L. Porter, D.O., PCNuclear/PET: Myocardial Perfusion Imaging4/30/2021
176William O. Sargeant, PC1758 Park Pl Suite 100MontgomeryAL36106William O. Sargeant, PCNuclear/PET: Myocardial Perfusion Imaging4/30/2021
187Mulberry Medical Associates1301 Mulberry StreetMontgomeryAL36106Mulberry Medical AssociatesNuclear/PET: Myocardial Perfusion Imaging4/30/2021
Sheet2


PQ getZip function
Code:
(address as text, city as text, state as text) as table =>

let
    Source = Table.FromColumns({Lines.FromBinary(Web.Contents("https://m.usps.com/m/QuickZipAction?mode=0&tAddress=" & address & "&tApt=&tCity=" & city & "&sState=" & state & "&jsonInd=Y"), null, null, 28591)}),
    Replace = Table.ReplaceValue(Source,"""","",Replacer.ReplaceText,{"Column1"}),
    Split = Table.SplitColumn(Replace, "Column1", Splitter.SplitTextByDelimiter("zip : ", QuoteStyle.Csv), {"Column1.1", "Column1.2"}),
    Remove = Table.RemoveColumns(Split,{"Column1.1"}),
    Rename = Table.RenameColumns(Remove,{{"Column1.2", "Zip"}}),
    CleanUp = Table.TransformColumns(Rename,{"Zip", each Text.Start(_,5)})
in
    CleanUp

PQ Table Transformations
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    ReplaceSpaces = Table.AddColumn(Source, "Custom", each Text.Replace([Street Address]," ","+")),
    GetZip = Table.AddColumn(ReplaceSpaces, "Zip", each getZip([Custom], [City], [State])),
    Expand = Table.ExpandTableColumn(GetZip, "Zip", {"Zip"}, {"Zip"}),
    Rename = Table.RemoveColumns(Expand,{"Custom"}),
    Reorder = Table.ReorderColumns(Rename,{"Order", "Practice Name", "Street Address", "Address line 2", "City", "State", "Zip", "Web link", "Accred1", "Accred1 Date"})
in
    Reorder
 
Upvote 0
That last Power Query function can be improved a bit. I dug around and found that Power Query can easily parse JSON. And it allows me to return the zip code value as just a text value instead of returning an entire table just for 1 bit of text. Same results as above just with some optimized code.

PQ Function
Code:
(address as text, city as text, state as text) as text =>

let
    Source = Table.FromColumns({Lines.FromBinary(Web.Contents("https://m.usps.com/m/QuickZipAction?mode=0&tAddress=" & address & "&tApt=&tCity=" & city & "&sState=" & state & "&jsonInd=Y"), null, null, 28591)}),
    zipValue = Table.AddColumn(Source, "Custom", each Json.Document([Column1])){0}[Custom][addresses]{0}[zip]
in
    zipValue

PQ Table Transformations
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    addZipColumn = Table.AddColumn(Source, "Zip", each getZip(Text.Replace([Street Address]," ","+"),[City],[State])),
    Reorder = Table.ReorderColumns(addZipColumn,{"Order", "Practice Name", "Street Address", "Address line 2", "City", "State", "Zip", "Web link", "Accred1", "Accred1 Date"})
in
    Reorder
 
Upvote 0
Also, I got home and hopped on Excel 365 and, admittedly pretty cool, it doesn't give you zip codes. It gives you cool info on city and state values though. Population, latitude, longitude, area, leaders, time zones, capital, household median income, etc.
 
Upvote 0
Thank you so much! You guys have been mentioning Power Query on this thread but I had never heard of it before. I have no idea how and where to paste that code. I did launch the Power Query editor, created a Blank Query, and pasted that last code into it, the one for the PQ getZip function. It appears to be saved. But I have no idea how to connect it with the data source or make it run yet. But ... baby steps. I'm on it. :)
 
Upvote 0
Is it necessary to have Power BI Desktop installed to use Power Query properly? Just curious.
 
Upvote 0
No, you don't need BI desktop. Depending on the version you have it can come pre-installed. I'm on mobile now so I can't do a step by step. Gimme an hour or so when I get to my desk I'll lay it out.

Don't forget about the VBA to get the zip code that I posted. Works just as well.
 
Upvote 0

Forum statistics

Threads
1,216,095
Messages
6,128,792
Members
449,468
Latest member
AGreen17

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