+ Reply to Thread
Results 1 to 13 of 13

Replace Leading Apostrophes

  1. #1
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,065

    Replace Leading Apostrophes

    I have a macro that's not doing all it's supposed to be doing.
    What it's supposed to do is remove all leading apostrophes.
    What it's doing is removing leading apostrophes from numbers, so '507 becomes 507, what I want.
    What it's NOT doing is removing leading apostrophes from text, so 'Doc becomes Doc, which I also want.

    What do I have wrong?

    Please Login or Register  to view this content.
    Attached Files Attached Files
    I know I'm not stupid, but I suspect I'm a lot stupider than I think I am

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,508

    Re: Replace Leading Apostrophes

    Maybe this:

    Please Login or Register  to view this content.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,065

    Re: Replace Leading Apostrophes

    Nope, that only worked on the numbers, same as my original macro.

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,453

    Re: Replace Leading Apostrophes

    I wonder if the code is doing anything you think it is. I added a Stop statement to the loop
    Please Login or Register  to view this content.
    and set watches for Len(s.value) and Instr(s.Value,"'"). Len() returns the string length without the apostrophe, and Instr() returns 0, indicating that the apostrophe character was not found in the string. I think your mistake is assuming that the apostrophe character is being seen as part of the stored text value. It appears to me that VBA is not seeing the apostrophe at all, so there is nothing happening in the Replace function. In the case of numbers stored as text, when VBA writes the number as text back to Excel, Excel decides to convert the number as text to a number.

    A little bit of further testing. I note that if I enter "Dog" into a blank cell, there is no leading apostrophe in the formula bar. If I add a leading apostrophe ("'Dog"), then the leading apostrophe is there, but the text otherwise behaves like "Dog" (LEN()=3 in both cases, for example). However, once I have entered the leading apostrophe, I cannot delete the apostrophe. Having once had the apostrophe entered, Excel seems to remember that text in this cell wants a leading apostrophe in the formula bar.

    Curious behavior that I have never explored before. The leading apostrophe does not appear to actually be part of the cell's value, but Excel is still aware that a cell has had a leading apostrophe in the past. From there, it needs more exploration to understand exactly what is happening.

    Can you move a step back and explain what you are trying to do here? Convert numbers stored as text to numbers? Is the leading apostrophe creating some other problem?
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  5. #5
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,065

    Re: Replace Leading Apostrophes

    Thanks for looking deeper. The only "problem" is Text numbers when I need real numbers, but an "annoyance" is where I have apostrophes show up and don't need them. But I also noticed the persistence issue, so maybe I'm looking for something I can't do.

    Also, I often get data that's a mix of both types, like
    Please Login or Register  to view this content.
    some of which has apostrophes and some doesn't. So I'd like to be able to just do away with any apostrophes and let the numbers be numbers and the text be text.

  6. #6
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,453

    Re: Replace Leading Apostrophes

    I tend to use the Text to Columns command for something like that.

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,508

    Re: Replace Leading Apostrophes

    Ok, try this:

    Please Login or Register  to view this content.
    It will, however, clear other formatting. Note that .PrefixCharacter is a Read-only Property.

  8. #8
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,065

    Re: Replace Leading Apostrophes

    Hmmm, that works, but it's kind of strange. Does a fine job on the text items, and it takes away the apostrophe from the numbers, but still leaves them as text numbers. Even if I copy them and paste on a new sheet as values, they're still text numbers. Easy enough to convert, but I haven't seen that before.

  9. #9
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,065

    Re: Replace Leading Apostrophes

    Thanks for pointing me in the right direction. I beefed it out a little so my numbers are numbers. I think this is going to work for me.
    Please Login or Register  to view this content.

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,508

    Re: Replace Leading Apostrophes

    I have seen lots of examples on the forum of data that looks, to all intents and purposes as numeric, but presents and acts as though it is text. And, to be honest, I have no idea why it happens.

    Maybe try:

    Please Login or Register  to view this content.

  11. #11
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,508

    Re: Replace Leading Apostrophes

    Quote Originally Posted by jomili View Post
    Thanks for pointing me in the right direction. I beefed it out a little so my numbers are numbers. I think this is going to work for me.
    Please Login or Register  to view this content.
    You're welcome. Thanks for the rep.

  12. #12
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,065

    Re: Replace Leading Apostrophes

    I guess we crossed, I didn't see your amended "Test" macro. wow, does what I want and much shorter! Thanks!

  13. #13
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,508

    Re: Replace Leading Apostrophes

    Quote Originally Posted by jomili View Post
    I guess we crossed, I didn't see your amended "Test" macro. wow, does what I want and much shorter! Thanks!
    Yep, guess we did. Enjoy!

+ 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] How to keep leading zeros when using find and replace
    By scottwiebe in forum Excel General
    Replies: 5
    Last Post: 11-15-2018, 04:01 PM
  2. [SOLVED] Trying to get rid of leading apostrophes in text cells
    By Habanero Time in forum Excel General
    Replies: 13
    Last Post: 12-29-2017, 08:12 PM
  3. Replies: 14
    Last Post: 09-26-2017, 02:05 PM
  4. Replies: 2
    Last Post: 02-09-2015, 12:32 PM
  5. Only one computer adding leading apostrophes to drop downs
    By dwayne2712 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-22-2012, 11:47 AM
  6. Replace leading zeros
    By brandnewday10 in forum Excel General
    Replies: 6
    Last Post: 09-01-2010, 10:14 AM
  7. [SOLVED] How do I remove leading apostrophes in Excel?
    By Nino in forum Excel General
    Replies: 8
    Last Post: 12-13-2005, 08:20 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