VBA - spaces

ericlch16

Active Member
Joined
Nov 2, 2007
Messages
311
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
Hello i have a string that might have multi spaces between the string but random spaces such as

test er <more than 1 space > kllk <more than 1 space > l
b <more than 1 space > w e <more than 1 space > tttttt klfdgd
klllll <more than 1 space > lll


* this thread is removing the multi spaces to 1 space. hence <the more than 1 space>

i wanted to replace all the spaces that are more than 1 to only 1 space so that the string becomes

test er kllk l
b w e tttttt klfdgd
klllll lll

I was going to going to use REPLACE function in VBA but this would not work as number of spaces varies. Any quick function i can use?
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
You can use the Excel Trim function like
VBA Code:
Application.Trim("a string")
 
Upvote 0
Solution
As long as they are normal spaces, the built-in Excel function TRIM will remove duplicate spaces.
 
Upvote 0
As long as they are normal spaces, the built-in Excel function TRIM will remove duplicate spaces.
The trim will only remove the space infront and at the end of a string. if there are 5 spaces between 2 words in a string, it will not change it to 1. for example string is tt<5 space>XX, the string will still be tt<5 space>XX after TRIM

I want to change the string to

tt<1 space>XX
 
Upvote 0
The trim will only remove the space infront and at the end of a string. if there are 5 spaces between 2 words in a string, it will not change it to 1. for example string is tt<5 space>XX, the string will still be tt<5 space>XX after TRIM

I want to change the string to

tt<1 space>XX
That is not correct. That is the VBA "TRIM" function, not the Excel "TRIM" function (note that I said "built-in Excel function TRIM").

If you want to use an Excel function in VBA, you can do so in the manner that Fluff posted.
 
Upvote 0
You need to use the Excel Trim function & not the VBA version as we said
 
Upvote 0
That is not correct. That is the VBA "TRIM" function, not the Excel "TRIM" function (note that I said "built-in Excel function TRIM").

If you want to use an Excel function in VBA, you can do so in the manner that Fluff posted.

thanks for the clarification. I will use the function instead. Great experts in this forum :)
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,573
Members
449,089
Latest member
Motoracer88

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