+ Reply to Thread
Results 1 to 9 of 9

Deleting leading and trailing spaces from a number

  1. #1
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2010, 2016
    Posts
    1,139

    Deleting leading and trailing spaces from a number

    I am using the following code to import an excel file into an existing sheet. The problem I am having is the way some of the cells are being imported. Some of the cells have spaces before and after the number. I need to delete the leading and trailing spaces for each cell affected. Is there a code I can incorporate to do this? BTW…the columns effected are columns F & G. Thanks

    Please Login or Register  to view this content.
    Last edited by maacmaac; 03-31-2009 at 10:35 PM.

  2. #2
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Deleting leading and trailing spaces from a number

    The Trim function removes leading and trailing spaces. You'd need to loop through the cells to apply it.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2010, 2016
    Posts
    1,139

    Re: Deleting leading and trailing spaces from a number

    I tried using the Trim Function in the code but no luck. I even tried to run the function on it's own for each cell in columns F, G, & H and that did not work either. I am stumped. I attached both files for review if needed. The file I am importing from has hyperlinks to each number but I am only pasting values. I'm not sure if that is part of the problem with the trim function not working. Thanks.
    Attached Files Attached Files

  4. #4
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Deleting leading and trailing spaces from a number

    Did you check to see if the leading/trailing characters are indeed blanks, and not, for example, non-breaking spaces?

  5. #5
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Deleting leading and trailing spaces from a number

    It is indeed a non-breaking space.

    In VBA, try
    Please Login or Register  to view this content.

  6. #6
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2010, 2016
    Posts
    1,139

    Re: Deleting leading and trailing spaces from a number

    Shg,

    Thank you for information. The VBA code is working as expected. One more question if you don't mind? How are you able to tell the difference between a "blank" and a "non-breaking space"? And what is the difference? Thanks again.

  7. #7
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Deleting leading and trailing spaces from a number

    A blank is character code 32. A non-breaking space is character code 160.

    I have an add-in that shows the ANSI character codes in a cell, and am happy to upload it if you're comfortable with add-ins.

    Otherwise, you can do =CODE(Mid(A1, x, 1)) to see the character code for any particular character x.

  8. #8
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2010, 2016
    Posts
    1,139

    Re: Deleting leading and trailing spaces from a number

    Thanks for the info. And yes, I would have an interest in the add-in. If you can upload it, that would be great. Thanks.

  9. #9
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Deleting leading and trailing spaces from a number

    I said I had this as an add-in, but I actually have it in Personal.xls. It comprises a form and a code module; can you sort it from there?

    I shorttcut the sub as Ctrl+Shift+B, mentally attached as Conveniently Show Binary.
    Attached Files Attached Files
    Last edited by shg; 03-31-2009 at 11:40 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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