Trim Function not working in VBA

gnaga

Well-known Member
Joined
Jul 9, 2002
Messages
748
Office Version
  1. 365
  2. 2016
  3. 2013
Platform
  1. Windows
I am using Trim Function in VBA to remove all spaces in a string stored in a cell.

My piece of code is like this


Code:
Adr = Application.WorksheetFunction.Trim(Cells(2, Target.Column))

Also I have tried this
Code:
 Adr = Trim(Cells(2, Target.Column))

But in both the cases I am getting the value into the variable Adr with the spaces as it is in the cell.

What could be the problem? I am breaking my head on this simple issue. :rolleyes:

TIA & Regards
GNaga
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi,

Application.WorksheetFunction.Trim will remove all leading, trailing & multiple internal spaces

Trim will remove all leading & trailing spaces

Application.Worksheetfunction.substitute(Cells(2, Target.Column)," ","")
will remove ALL spaces.

HTH

Alan
 
Upvote 0
Thank you Alan.

Your solution using substitute works fine.

One thing I could'nt get you. What is the meaning of multiple internal spaces? My string also have multiple spaces between words but not on the leading and trailing ends.

Thank you so much for your timely help.

Regards
GNaga
 
Upvote 0
Hi

If you had a string:
[space] A [space] [space] B [space]

Application.Worksheetfunction.Trim would convert it to
A [space] B

But Trim would convert it to
A [Space] [Space] B

HTh

Alan
 
Upvote 0

Forum statistics

Threads
1,221,487
Messages
6,160,114
Members
451,619
Latest member
KunalGandhi

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