

This is an Auxiliary function that loads the sheet into a collection Function LoadInCol() As Collection Just take care it will remove the format of your text, i also did not do much testing and it's exhaustive but it worked for my short task and worked fast.
#How to remove spaces between words in excel code#
If You are familiar with collections, i once wrote a quick code that process the whole sheet even if it is huge and remove all double spaces, lead and trail spaces and invisible characters from all cells.

' Whitespace and all spaces between words are removed.ĭebug.Print "Character Count: " & Len(CleanUltra(myVar, True)) ' Optional parameter to remove spaces between words is set to TRUE. ' Whitespace is removed but spaces between words are preserved.ĭebug.Print "Character Count: " & Len(CleanUltra(myVar)) ' remove spaces between words is left blank it defaults to FALSE. ' Examples of various types of whitespaceĭebug.Print "Character Count: " & Len(myVar) I had to set the function to remove it first, before the CLEAN and TRIM functions were used to stop them from removing all characters after the vbNullChar. Here's a test I ran to verify that the function actually removed all whitespace. Here's an example of it's usage: Sub Example() This function deletes all spaces except for single spaces between words. Choose one or all of the following options: Trim leading and trailing spaces. In Excel, to remove the leading, trailing, and extra spaces between words in a particular string, use the TRIM function. Click the Trim Spaces button on the ribbon. Select the cell(s) where you want to delete spaces. StringToClean = Replace(stringToClean, " ", vbNullString) Trim Spaces for Excel remove extra spaces in a click. If removeSpacesBetweenWords = True Then _ StringToClean = Application.Trim(stringToClean) ' Remove all spaces except single spaces between words StringToClean = Application.Clean(stringToClean)
/excel-2013-trim-function-formula-5ab02abe8023b90036cbd837.jpg)
StringToClean = Replace(stringToClean, vbNullChar, vbNullString) ' Removes non-printable characters and whitespace from a string Optional ByVal removeSpacesBetweenWords As Boolean = False) _ Any improvements are welcome! Function CleanUltra( _ Are all your other functions leaving whitespace behind?ĬleanUltra removes all whitespace and non-printable characters including whitespace left behind by other functions!
