+ Reply to Thread
Results 1 to 16 of 16

How do I strip punctuation from end of text?

  1. #1
    Registered User
    Join Date
    04-26-2011
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    48

    How do I strip punctuation from end of text?

    Hi All. I have a macro that should strip the punctuation from the end of text in a range. When I run the macro it eliminates the text as well as the punctuation. Can someone please look at this code and tell me what I;m doing wrong? Thank you so much. I am including a spreadsheet with sample data. The code is below.

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by egavasrg; 11-07-2011 at 10:36 AM.

  2. #2
    Valued Forum Contributor ffffloyd's Avatar
    Join Date
    05-20-2008
    Location
    Perth, WA, Australia
    MS-Off Ver
    Office 365
    Posts
    249

    Re: How do I strip punctuation from end of text?

    A question mark replaces any text. To replace an actual question mark, precede it with a tilde, viz: "~?". Even that is not going to remove punctuation from the end of your text though, but from anywhere in the text.
    _______________
    Floyd Emerson
    Business Intelligence Consultant
    Perth, Western Australia

  3. #3
    Registered User
    Join Date
    04-26-2011
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: How do I strip punctuation from end of text?

    Thanks ffffloyd (I feel like I'm stuttering when I type that). I'll give that a try. Will it work with the other punctuation marks as well?

    RS

  4. #4
    Registered User
    Join Date
    04-26-2011
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: How do I strip punctuation from end of text?

    Wow!!! Thank you so much. That is exactly what I needed. I used the tilde (~) on all the punctuation elements I wanted to remove and it worked. Thanks again. This was fantastic.

    RS

  5. #5
    Valued Forum Contributor ffffloyd's Avatar
    Join Date
    05-20-2008
    Location
    Perth, WA, Australia
    MS-Off Ver
    Office 365
    Posts
    249

    Re: How do I strip punctuation from end of text?

    I have attached your test workbook with some modifications to Sub StripPunctuation that will remove punctuation only from the end of your text as you had asked. The code you have written will remove punctuation from anywhere in the text (which may be your intention but not what you asked).

    There are two methods. Obviously, you only need to code one of them. Method 1 removes anything that is not alphanumeric, so it will get all punctuation. Method 2 removes only the specific punctuation that you set. That also answers your question about whether it will work on other punctuation marks as well.
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    04-26-2011
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: How do I strip punctuation from end of text?

    Hi ffffloyd. Thanks for the additional effort on this. Your code is really elegant but way beyond my current skill level. I'm very new to vba and I have a difficult time following variable in i, c, s form. You may have noticed that I named my variables with words. Someone told me that's pretty common for beginners like me. That said, I will study your code. I'm sure I will learn a lot from stepping through it to see exactly what each of the variable do. I'm very interested in how you used the constant. I've never seen that done like that before. Hopefully I'll be able to write code like that one day. It took me a couple of hours just to put together what I sent you. I really appreciate the expertise guys like you bring to this forum. Take care.

    RS

  7. #7
    Registered User
    Join Date
    04-26-2011
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: How do I strip punctuation from end of text?

    Hi ffffloyd. I know this issues is solved, but I'd like to ask a favor. As I stated, I'm really new at vba and have a hard time following variables that are only one letter. Would you mink adding a comment next to the variable s, i, and c so I can better understand what the code is doing. It would really help me out. Thanks in advance, and if you can't, I still appreciate your help on this problem. Have a great day.

    RS

  8. #8
    Valued Forum Contributor ffffloyd's Avatar
    Join Date
    05-20-2008
    Location
    Perth, WA, Australia
    MS-Off Ver
    Office 365
    Posts
    249

    Re: How do I strip punctuation from end of text?

    Sorry for my shorthand. I've uploaded the same code using longer variable names.

    Basically: s (now sValue) is a string to hold the value of the cell for processing; i (now iIndex) is an index into the value string to extract characters one by one from the right hand side; and c (now sChar) is the character that has been extracted from sValue according to the value of iIndex. VBA does not have a single Character type so we are forced to use a String type and limit it to one character at a time.

    The constant serves two purposes. Firstly, if it is set to an empty string then the code does not search for specific punctuation marks but assumes that anything which is neither alphabetic nor numeric will need to be removed. Note that it takes the upper case of the character so that if it is alphabetic then it only needs to check the range "A" to "Z"; it will never be "a" to "z".

    The second purpose of the constant is to hold particular punctuation marks if those characters alone need to be removed. It takes one character at a time from the right hand side of the value and checks that character's position within the constant. If a value greater than zero is returned then it knows the character is among those which are disallowed. However, if a zero is returned then the character was not found in the constant and therefore must be allowed; hence the strip back has ended.

    When it exits the For loop, whatever index it has counted down to is the length of the allowed string, so it takes everything up to that point as the new value.

    Anyhow, take a look at the new upload. I hope it is clearer now.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    04-26-2011
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: How do I strip punctuation from end of text?

    Wow, ffffloyd! You should be teaching this stuff!!! This is totally clear now. I really appreciate you taking the extra time to explain for me. Now I will be able to modify this code for other uses since I know how it is constructed and what it does. Thank you so much. Have a great day.

    RS

  10. #10
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: How do I strip punctuation from end of text?

    Based on the example you posted:

    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.
    Last edited by snb; 11-06-2011 at 12:55 PM.



  11. #11
    Registered User
    Join Date
    04-26-2011
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: How do I strip punctuation from end of text?

    Thanks SNB. The first code removes all of the text. I didn't try the second one because it looks like it will remove the last letter of the first name. Thanks for your recommendations, though. As this point I have everything I need to make this work in the way I need it to work. Again, I appreciate your effort on this but it is SOLVED at this point. Thanks.

    RS

  12. #12
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: How do I strip punctuation from end of text?

    It's impossible that all text will be removed when using:

    Please Login or Register  to view this content.
    Some code is easier to adapt to other uses than other code.
    Last edited by snb; 11-06-2011 at 01:57 PM.

  13. #13
    Registered User
    Join Date
    04-26-2011
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: How do I strip punctuation from end of text?

    I'm sure you're right. That said, I'm getting an interesting result when I run that code. I'm attaching a CamStudio capture for your review. Thanks.

    RS
    Attached Files Attached Files

  14. #14
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: How do I strip punctuation from end of text?

    This is an Excelforum.
    Please post Excel-files.

  15. #15
    Valued Forum Contributor ffffloyd's Avatar
    Join Date
    05-20-2008
    Location
    Perth, WA, Australia
    MS-Off Ver
    Office 365
    Posts
    249

    Re: How do I strip punctuation from end of text?

    Quote Originally Posted by snb View Post
    It's impossible that all text will be removed when using:

    Please Login or Register  to view this content.
    Some code is easier to adapt to other uses than other code.
    SNB, the reason this code will remove all text is that a question mark matches any character when using Replace. In order to replace an actual question mark, you need to specify "~?". The tilde (~) acts as an escape character and tells the parser to interpret the next character exactly as is.

  16. #16
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: How do I strip punctuation from end of text?

    Thanks for pointing that out to me

    resulting in

    Please Login or Register  to view this content.
    But if only the last puctuation should be removed I'd prefer the oneliner :

    Please Login or Register  to view this content.
    Last edited by snb; 11-07-2011 at 04:52 AM.

+ 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