VBA for Trim

normpam

Active Member
Joined
Oct 30, 2002
Messages
354
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

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
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,214,593
Messages
6,120,435
Members
448,961
Latest member
nzskater

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