+ Reply to Thread
Results 1 to 13 of 13

VBA Trim Leading Spaces

  1. #1
    Registered User
    Join Date
    01-30-2020
    Location
    UK
    MS-Off Ver
    2013
    Posts
    82

    VBA Trim Leading Spaces

    Hi All

    I have source data that contains leading spaces in some blank cells. I've adapted the below to try and remove them using an array, but instead it is wiping column A. Could you help tweak it please?

    Many thanks!

    Please Login or Register  to view this content.
    Last edited by warp765; 04-16-2020 at 11:42 AM.

  2. #2
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,080

    Re: VBA Trim Leading Spaces

    You need to remove this line
    Please Login or Register  to view this content.
    It's resetting the array, which means it will then be blank.

  3. #3
    Forum Expert BMV's Avatar
    Join Date
    01-13-2020
    Location
    St-Petersburg, Russia
    MS-Off Ver
    2013-2016
    Posts
    1,329

    Re: VBA Trim Leading Spaces

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    01-30-2020
    Location
    UK
    MS-Off Ver
    2013
    Posts
    82

    Re: VBA Trim Leading Spaces

    Many thanks @Fluff13 and BMV for your help, all sorted. Cheers

  5. #5
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,080

    Re: VBA Trim Leading Spaces

    You're welcome & thanks for the feedback

  6. #6
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: VBA Trim Leading Spaces

    Since you are using a fixed range, and if you do not mind using Excel's TRIM function (multiple adjacent internal spaces will be collapsed down to single spaces), then here is a one-liner that you can use...
    Please Login or Register  to view this content.

  7. #7
    Forum Expert BMV's Avatar
    Join Date
    01-13-2020
    Location
    St-Petersburg, Russia
    MS-Off Ver
    2013-2016
    Posts
    1,329

    Re: VBA Trim Leading Spaces

    @Rick Rothstein Worksheet trim delete all multi spaces but VBA trim only Leading Spaces. However Evaluate could be used.
    Last edited by BMV; 04-16-2020 at 03:34 PM.

  8. #8
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: VBA Trim Leading Spaces

    @BMV,

    All spaces? I did qualify my suggestion by including in parentheses this... "multiple adjacent internal spaces will be collapsed down to single spaces" which is what I think you meant when you said "all". I used the square bracket version of Evaluate because the OP appeared to indicate the range was fixed in size.
    Last edited by Rick Rothstein; 04-16-2020 at 03:12 PM.

  9. #9
    Forum Expert BMV's Avatar
    Join Date
    01-13-2020
    Location
    St-Petersburg, Russia
    MS-Off Ver
    2013-2016
    Posts
    1,329

    Re: VBA Trim Leading Spaces

    @Rick Rothstein sorry i miss "multi". and i mind trailing spaces will be deleted also.

  10. #10
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: VBA Trim Leading Spaces

    Quote Originally Posted by BMV View Post
    ...and i mind trailing spaces will be deleted also.
    Yes, I should have probably mentioned that one, but when I read this in the OP... "I have source data that contains leading spaces in some blank cells"... it made it sound like there were no trailing spaces to worry about. My assumption was that someone added leading spaces to an original text file to fill out a field of fixed length and it was those leading spaces the OP had to contend with when it was brought into Excel. I probably over-thought this one a little too much although it would have been helpful if the OP had included a sample file.

    As to the original question for a left trim only, we can still do that without any loops (still a one-liner) and, again, assuming a fixed range of cells...
    Please Login or Register  to view this content.

  11. #11
    Forum Expert BMV's Avatar
    Join Date
    01-13-2020
    Location
    St-Petersburg, Russia
    MS-Off Ver
    2013-2016
    Posts
    1,329

    Re: VBA Trim Leading Spaces

    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    01-30-2020
    Location
    UK
    MS-Off Ver
    2013
    Posts
    82

    Re: VBA Trim Leading Spaces

    WOW! Thanks all, it was leading spaces only for me but great to see so many options!

    Many thanks again.

  13. #13
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: VBA Trim Leading Spaces

    @BMV,

    I needed to add what is shown in red in order to make your code work correctly on my XL2010 worksheet...
    Please Login or Register  to view this content.
    Last edited by Rick Rothstein; 04-16-2020 at 08:47 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] trim to match two sets of input-trim spaces around a character
    By nigelog in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-21-2017, 12:33 PM
  2. [SOLVED] TRIM leading character
    By lemonap618 in forum Excel General
    Replies: 3
    Last Post: 01-20-2016, 11:01 PM
  3. [SOLVED] trim does not remove leading spaces?
    By inventorgeorge in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 11-28-2015, 01:19 PM
  4. TRIM function and leading spaces
    By nsv in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 10-01-2014, 10:32 PM
  5. Delete Trailing Spaces without Removing leading zeros WITHOUT TRIM
    By Zach51215 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-20-2013, 04:24 PM
  6. trim leading spaces
    By captain bob in forum Excel General
    Replies: 2
    Last Post: 08-03-2006, 10:00 AM
  7. trim leading spaces
    By [email protected] in forum Excel General
    Replies: 10
    Last Post: 05-11-2006, 04:25 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1