From the course: Excel VBA: Managing Files and Data

Unlock this course with a free trial

Join today to access over 24,900 courses taught by industry experts.

Remove spaces from before or after a string

Remove spaces from before or after a string

From the course: Excel VBA: Managing Files and Data

Remove spaces from before or after a string

- [Instructor] One common problem with data that you bring in from outside of Microsoft Excel is that there are spaces before and after the values that you import as strings. In this movie, I will show you how to use keywords in Microsoft Excel VBA to get rid of those extra spaces. My sample file is 04_05_TrimmingText, and you can find it in the Chapter04 folder of the Exercise Files collection. I won't use any of the worksheet data in this movie, so I'll just press Alt+F11 to move to the Visual Basic Editor. In my code module, I have a subroutine called TrimText, and at the top, I have declared four variables, all strings. And below I have assigned values to each of those string variables. The first, sText1, has extra spaces to the left of the string. The second has spaces to the right. And the third has extra spaces to the left and right. So I'll show you how to handle those. And I'll do that within the context of the variable assignment statement for all the text, which brings…

Contents