TRIM in VBA on a Range

ilcaa

Well-known Member
Joined
May 25, 2005
Messages
751
Office Version
  1. 365
Platform
  1. Windows
i have a range with 6000 rows, i want to apply a worksheet.TRIM function within VBA to eliminate spaces in-between and on the ends...

problem is... nothing happens. whats best syntax for this? thanks

<code>

set rng = range("a1:a6000")

With rng

.value = WorksheetFunction.Trim(.value)

End With


</code>
 
Just for fun, this also can be used:
Rich (BB code):
Sub TrimRange()
  With Range("A1:A6000")
    .Value = Application.Trim(.Value)
  End With
End Sub
:)
And, since the range is fixed in size, either of these one-liners will also work...
Code:
Sub TrimRange()
  [A1:A6000] = [IF({1},Trim(A1:A6000))]
End Sub
Code:
Sub TrimRange()
  [A1:A6000] = Application.Trim([A1:A6000])
End Sub
 
Upvote 0

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Or, for anyone using Office 365
Code:
Sub TrimRange()
  [A1:A6000] = [Trim(A1:A6000)]
End Sub
 
Upvote 0
Or, for anyone using Office 365
Code:
Sub TrimRange()
  [A1:A6000] = [Trim(A1:A6000)]
End Sub
Office 365 does not require the array processing to be induced into text functions? When I run your code line in XL2010, it clears all of the cells which is why I used the IF function call that I did in my first macro (Message #11 ) in order to induce the array processing.
 
Upvote 0
Whilst I still use the If function, as most people are not using 365, it seems that it's no longer needed. I've tried it with various different formulae & it works without the If.
I'm guessing that It's got something to do with the new array functions, even though I don't have them yet.
 
Upvote 0
Whilst I still use the If function, as most people are not using 365, it seems that it's no longer needed. I've tried it with various different formulae & it works without the If.
I'm guessing that It's got something to do with the new array functions, even though I don't have them yet.
Interesting... well atleast Microsoft is maintaining backward compatibility.
 
Upvote 0

Forum statistics

Threads
1,224,747
Messages
6,180,712
Members
452,995
Latest member
isldboy

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