Tidy up Addresses- VBA

Noni

Board Regular
Joined
Aug 27, 2022
Messages
63
Office Version
  1. 2021
Platform
  1. Windows
Hi, how can below codes can be modified to clean the addresses? Suburb name and State names have unwanted spaces and messy.
WorksheetDummy.xlsm
AB
1Raw AddressCleaned Address
21 Co Street WALLA WAL LA NS W1 Co Street WALLA WALLA NSW
3 Lock Cres MILB AC T Lock Cres MILB ACT
4 cnr March St/June Sts HENTY N SW cnr March St/June Sts HENTY NSW
5 4 Apra Street ALBURYQ LD 4 apra Street ALBURY QLD
6 Cnr Bu St & Ur Road LA VINGTON N T Cnr Bu St & Ur Road LAVINGTON NT
7 May Road LAVINGTON NSW May Road LAVINGTON NSW
8 1 Octo Street JIND ERA W A 1 OctoStreet JINDERA WA
9 4 Nov Road JINDERA S A 4 NovRoad JINDERA SA
10 7 April Court BURRUM CK T AS 7 April Court BURRUMCK TAS
Sheet3

VBA Code:
Function TidySpaces(s As String) As String
  Dim RX As Object
  Dim Pat As Variant
 
  Const Patterns As String = "([a-z])( )([a-z])#(\d)( )(\d)#(\/)( )(.)#([A-Z])( )([A-Z])#([A-Z])( )([a-z])"
 
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  For Each Pat In Split(Patterns, "#")
    RX.Pattern = Pat
    s = RX.Replace(s, "$1$3")
  Next Pat
  TidySpaces = s
End Function
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
'Cleaning' usually means a set of rules to decide which part of a string is to be change, and which part is to be left alone. What are your rules for deciding when a space is retained, and when it is removed? For example, take this raw address: 1 Co Street WALLA WAL LA NS W. How is the code to know that the space between "WAL" and "LA" is to be removed, but the space between "Street" and "WALLA" should be retained? What is your rule?
 
Upvote 0
'Cleaning' usually means a set of rules to decide which part of a string is to be changed, and which part is to be left alone. What are your rules for deciding when a space is retained, and when it is removed? For example, take this raw address: 1 Co Street WALLA WAL LA NS W. How is the code to know that the space between "WAL" and "LA" is to be removed, but the space between "Street" and "WALLA" should be retained? What is your rule?
 
Upvote 0
As a further example, NSW has a town "GIN GIN" while WA has "GINGIN". Without a complete list of names and states to compare against, there would be no way to decide if the space in "GIN GIN" should be removed or not.
 
Upvote 0
As a further example, NSW has a town "GIN GIN" while WA has "GINGIN". Without a complete list of names and states to compare against, there would be no way to decide if the space in "GIN GIN" should be removed or not.
Name of states/ territories are NSW, WA, SA, ACT, TAS, NT, VIC. Addresses are endless and may be different every year. So I think a rule can be created only on state/ territories.
 
Upvote 0
Name of states/ territories are NSW, WA, SA, ACT, TAS, NT, VIC.
.. and QLD??

So I think a rule can be created only on state/ territories.
The issue is: What is the rule?
As I said before, without a complete list of every possible location in every state and territory, there is no way to know whether to remove a space from the location name or not.
For another example Is "GREEN HILLS" correct as it is or could it be "GREENHILLS"? More importantly, how is Excel to know which one is correct?
 
Upvote 0
Another option if you have access to a 'Google API key' could be to pass your current address through googles geocoding service and then output the FullAddress part of the return. This way you would have the addresses as google sees them. Like I said just an option if you have a Google API key.
 
Upvote 0
Is there any chance that when extracting ( sourcing) the raw addresses(into excel) that some form of delimitation was applied, it would be of interest to see what the source data was before importing to excel if that is possible. Just seems strange that the states and cities are the ones being split abnormally
 
Upvote 0

Forum statistics

Threads
1,214,998
Messages
6,122,639
Members
449,093
Latest member
Ahmad123098

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