VBA for Trim

normpam

Active Member
Joined
Oct 30, 2002
Messages
355
If I have 10x16 in a cell with a space after it and I use a VBA code For each cell.... cell.value = trim(cell.value) it works just fine. BUT.... if I have text in a cell like ABC terminology access, and there is an extra space after the ABC, then if I am in the spreadsheet itself and use the trim formula, it works just fine. But if I use the same exact VBA command, it completely ignores it and does NOT take out the extra space. Is there some type of quirk with the VBA Trim function?
 

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.
I don't know why it works that way, but the VBA version of trim only removes extra spaces from the beginning & end of a string. It does not remove them from the middle of the string.
You can however use
VBA Code:
Application.Trim
instead
 
Upvote 0
I don't know why it works that way, but the VBA version of trim only removes extra spaces from the beginning & end of a string. It does not remove them from the middle of the string.
You can however use
VBA Code:
Application.Trim
instead
So, it would be For Each Cell In Range(something) cell.value = application.trim.value?
 
Upvote 0
It would need to be
VBA Code:
cell.value = application.trim(cell.value)
 
Upvote 0

Forum statistics

Threads
1,215,339
Messages
6,124,370
Members
449,155
Latest member
ravioli44

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