Powerful Excel VBA Macro to TRIM Selection - 2268

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 Feb 28, 2019.
Kristin wants to have a fast way to remove leading a trailing spaces from an entire range of Excel cells. In this episode, a 7-line macro to solve this problem.
Sub TrimSelection()
Dim cell As Range
For Each cell In Selection
If Not cell.HasFormula Then
cell.Value = Trim(cell.Value)
End If
Next cell
End Sub
maxresdefault.jpg


Transcript of the video:
Learn Excel from Mr. Excel podcast 2268. VBA Macro to trim all cell and selections.
Alright. So I was doing a power excel seminar and Kirstin, Kirstin said, “Hey, we’re talking about VLOOKUP and one of the common things that causes VLOOKUP to not work is if your data has trailings basis.” Alright, so right here instead of just Kiwi, I have Kiwi space, space and that’s causing things to not work. And I usually suggest coming out here and using the trim function. Equal trim, that whole thing or even using the trim function inside. But Kirstin just said she’s downloading data and she has multiple columns that all need to be trimmed every single time.
And if there’s some each way to do this.
I said, “Well what we could do is, we could knock out a quick little macro, a VBA Macro.” You know, hey, I always tell people I use Power Query 90% of the time instead of macros.
But every once in a while macro is really, really good.
Alright, so Alt F11 and we have this, we have this little macro right here. Sub, trim selection and I dim cell is arranged. For each cell in selection, cell.value equals the trim of the cell.value. But I don’t want to screw up any formulas, right incase someone space their formulas out, I’ve seen people do that. So if not, cell. Formula then do that whole thing. So that’s trim selection here.
Let’s come back and we’ll customize the quick access tool bar, go to macros and find personal xls trim selection right there, click add, I already added it and then modify to come up with a nice little icon there and nice words of trim selections.
Alright, so now let’s check this out. Here’s how it works. We choose all the cells that we want to trim. And then the trim gets rid of—if you have multiple spaces between two words, trim, we’ll get rid of those, alright.
So I’m going to just trim the selection here. That one. And all of our VLOOKUP starts to work. Wrong format, but other than that, although, really interesting, the VBA version of trim doesn’t rid of the extra spaces inside. It got rid of the leaving spaces and the trailing spaces but not the spaces inside.
And very nice, it didn’t touch our formulas.
So that little VBA there for Kirstin, I want to thank you for stopping by. We’ll see you next time for another netcast from Mr.
Excel.
 

Forum statistics

Threads
1,221,558
Messages
6,160,487
Members
451,651
Latest member
Penapensil

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