Learn Excel 2010 - "Remove Spaces with VBA": Podcast #1465

If you like this content, please consider visiting the video on YouTube and subscribe to the MrExcel Channel to show your support!
This video has been published on Nov 7, 2011.
Gary points out that the TRIM Function in VBA isn't working like =TRIM() Function in the worksheet; the spaces in the middle remain after running the Macro. Bill shows us, In episode #1465, how to solve this issue with the 'Application.WorksheetFunction' to remove those spaces in the middle.

...This blog is the video podcast companion to the book, Learn Excel 2007 through Excel 2010 from MrExcel. Download a new two minute video every workday to learn one of the 512 Excel Mysteries Solved! and 35% More Tips than the previous edition of Bill's book!

"The Learn Excel from MrExcel Podcast Series"

MrExcel.com — Your One Stop for Excel Tips and Solutions. Visit us today!
maxresdefault.jpg


Transcript of the video:
MrExcel podcast is sponsored by Easy-XL.
Learn Excel from MrExcel podcast, episode 1465: VBA trim.
Hey, welcome back to the MrExcel netcast.
I’m Bill Jelen.
Today's question is sent in by Gary.
Gary has some data that has leading spaces, trailing spaces, and spaces in the middle.
All right, so, the TRIM function in Excel will get rid of the leading spaces, the trailing spaces, and also the extra spaces in the middle.
So, if you have ABC space, space, space DEF, that'll reduce that down to one.
So, Gary was writing a macro in VBA, Alt F11-- let’s see here.
Insert, module, let’s call it Gary one-- no, let’s call it Gary.
Yes, for each cell in selection, cell dot value is equal to the trim of cell dot value.
Next.
So, let's select this whole range, the exact same data we had over there in column A. We'll run this macro and Gary is amazed to find out that the VBA trim gets rid of the leading spaces and the trailing spaces, but does not get rid of the extra spaces in the middle.
He's like, “What's up with that?
Why doesn't that work?” Okay, so, TRIM existed in VBA long ago; long before VBA was ever incorporated into Excel.
All right, so, what we're going to do here is we're going to instead of using the VBA version of TRIM, we’re going to use the Excel version of TRIM.
So, cell dot value is equal to application dot worksheet function dot-- and I'm holding my breath to hope that it's here.
See, they don't have all 400 Excel calculation functions here, but they have a lot of them.
If the Excel team did the right thing, they gave us their TRIM.
There it is.
Cell dot value.
So, what this is going to do, this is going to run the Excel version of TRIM instead of the VBA version of TRIM.
Let's select that range and there we go.
It goes back and does the right thing.
So, today we're talking about application dot worksheet function.
If you need to rock out something that Excel does that VBA doesn't seem to do, try that and see if you're lucky enough that the function you need is in that list.
Well, hey, I want to thank you for stopping by.
We'll see you next time for another netcast from MrExcel.
 

Forum statistics

Threads
1,223,721
Messages
6,174,097
Members
452,542
Latest member
Bricklin

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