Removing blanks between text

Noni

Board Regular
Joined
Aug 27, 2022
Messages
63
Office Version
  1. 2021
Platform
  1. Windows
HI all,

How can I remove blanks between words in a cell? How can I change below raw address into cleaned address? There is one blank between 123, one blank between Stre and et, 3 blanks between VIC and 21 and one blank between 21 and 30.
Raw AddressCleaned address
12 3 Maxwell Stre et VIC 21 30 123 Maxwell Street VIC 2130
 
Just to confirm, the logic of my udf is to remove a space if it occurs between
  • Two lower case letters
  • Two upper case letters
  • An upper case letter followed by a lower case letter
  • Two digits
  • A "/" character and anything else (following)
 
Upvote 0

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
@Noni
You have added quite a lot of 'Likes' to the thread - does that mean that that something is actually working for you?
 
Upvote 0
Just to confirm, the logic of my udf is to remove a space if it occurs between
  • Two lower case letters
  • Two digits
  • A "/" character and anything else (following)
  • Two upper case letters
  • An upper case letter followed by a lower case letter
I want leading and trailing spaces to be removed as well, in case if there is any
 
Upvote 0
I want leading and trailing spaces to be removed as well, in case if there is any
Try this. As well as leading and trailing spaces it should also remove multiple internal spaces like in your original example the double space between 56 and Spring in "56 Spring Cres QLD 8767"

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 = Application.Trim(RX.Replace(s, "$1$3"))
  Next Pat
  TidySpaces = s
End Function
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,874
Members
449,056
Latest member
ruhulaminappu

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