+ Reply to Thread
Results 1 to 9 of 9

VBA function to remove hypens, spaces, leading zeros

  1. #1
    Registered User
    Join Date
    04-12-2006
    Posts
    39

    VBA function to remove hypens, spaces, leading zeros

    Hi,
    I have searched but can't seem to find exactly what I need to accomplish this simple task. I want to
    1. Strip all leading spaces (could be any number of spaces) e.g. " 12345" or " 12345"
    2. Strip all leading zeros (could be any number of zeros) e.g "012345" or "00012345" or " 012345"
    3. Strip all spaces in the string (could be anywhere in the string and any number of spaces) e.g. "12 34 56 " or " 1"
    4. Strip all Hyphens in the string (could be anywhere in the string and any number of hyphens)

    I've managed to cobble this code together that does most of what I need but it wont handle the case where I have a leading space and a leading zero. (e.g. " 012345"). In this example the leading space gets removed but the zero doesn't. I've tried adding a "Trim" but I just can't get it to work.

    What code do I need to add to remove the leading space where there is also a leading zero and where do I need to add it?
    My Code
    Please Login or Register  to view this content.

  2. #2
    Forum Contributor
    Join Date
    03-08-2011
    Location
    London
    MS-Off Ver
    Work Office 365 Home 2019
    Posts
    428

    Re: VBA function to remove hypens, spaces, leading zeros

    Have you thought about adding a value that is " 0", ""?

  3. #3
    Forum Contributor
    Join Date
    03-08-2011
    Location
    London
    MS-Off Ver
    Work Office 365 Home 2019
    Posts
    428

    Re: VBA function to remove hypens, spaces, leading zeros

    Have you thought about adding a value that is " 0", so you get
    Please Login or Register  to view this content.

  4. #4
    Forum Contributor
    Join Date
    03-08-2011
    Location
    London
    MS-Off Ver
    Work Office 365 Home 2019
    Posts
    428

    Re: VBA function to remove hypens, spaces, leading zeros

    Have you thought about adding a value that is " 0", so you get
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    04-12-2006
    Posts
    39

    Re: VBA function to remove hypens, spaces, leading zeros

    That would only solve one case. There could be any number of spaces at the beginning

    Quote Originally Posted by tbgb View Post
    Have you thought about adding a value that is " 0", ""?
    Last edited by ssjody; 12-07-2014 at 07:48 AM. Reason: formatting issue

  6. #6
    Forum Contributor
    Join Date
    03-08-2011
    Location
    London
    MS-Off Ver
    Work Office 365 Home 2019
    Posts
    428

    Re: VBA function to remove hypens, spaces, leading zeros

    Oh right, just a thought, trust me I'm no expert, perhaps if you got it to loop that might first remove the spaces then remove the 0's

    I'm sure there are others on here who will come up with a much more elegant solution.

    Also, what happens if you have a number that contains a 0 e.g. 12 30 45?
    Last edited by MagicMan; 12-07-2014 at 08:09 AM.

  7. #7
    Forum Contributor
    Join Date
    03-08-2011
    Location
    London
    MS-Off Ver
    Work Office 365 Home 2019
    Posts
    428

    Re: VBA function to remove hypens, spaces, leading zeros

    Removed as duplicate posting

  8. #8
    Registered User
    Join Date
    08-20-2012
    Location
    Londonish, England
    MS-Off Ver
    Excel 2010
    Posts
    58

    Re: VBA function to remove hypens, spaces, leading zeros

    Hi ssjody,

    I use a formula I got from someone on here to remove trailing spaces from cells normally, I'm not sure if you could adapt it somehow into your macro?

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Thanks

    Chriz

  9. #9
    Registered User
    Join Date
    04-12-2006
    Posts
    39

    Re: VBA function to remove hypens, spaces, leading zeros

    Thanks, I tried to get something similar to work but I'm to much of a novice to have any success. I'm pretty sure my problem is that since the space precedes the zero that i only gets a length of one. I've tried moving the "mid" to occur after the replace but I can't figure out the syntax to get it to compile.

    Quote Originally Posted by Chriz View Post
    Hi ssjody,

    I use a formula I got from someone on here to remove trailing spaces from cells normally, I'm not sure if you could adapt it somehow into your macro?

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Thanks

    Chriz

+ 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. 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
  2. Remove leading zeros from a function
    By elbmag in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-19-2007, 01:27 AM
  3. Remove Leading Spaces
    By edwardpestian in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-22-2006, 10:34 AM
  4. VBA help please - remove leading spaces
    By Scott Wagner in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-24-2006, 04:20 PM
  5. Remove Leading Spaces
    By Kirk P. in forum Excel General
    Replies: 6
    Last Post: 03-09-2005, 12:10 AM

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