+ Reply to Thread
Results 1 to 10 of 10

Find a Last Occurrence of a String

  1. #1
    Forum Contributor
    Join Date
    06-17-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    157

    Find a Last Occurrence of a String

    I am a veteran of Excel VBA, but VBA in Word seems completely foreign to me. Perhaps you can help me.

    I have a word document with several occurrences of the string "BLOCK|". Is there a way to find the last occurrence in the document, then move down two lines and select to the end of the the line and replace the text " X |" with " |"? I am so lost with Word VBA, so any help is appreciated.
    Last edited by Ricker090; 04-27-2013 at 08:33 AM. Reason: Solved

  2. #2
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,170

    Re: Find a Last Occurrence of a String

    Unless you're dealing with a table, you can do this without a macro, using a wildcard Find/Replace. However, you haven't provided enough information about what's in the two lines between "BLOCK|" and " X |". Are these lines separate paragraphs and, if so, what do they contain?
    Cheers,
    Paul Edstein
    [MS MVP - Word]

  3. #3
    Forum Contributor
    Join Date
    06-17-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    157

    Re: Find a Last Occurrence of a String

    I believe these lines are considered separate paragraphs. The are separated by a return carriage. The line below "BLOCK|" is a series of "|---------" that repeat a number of times (which I wish to bypass). Below that is a series of " X |" which repeats a different number of times depending on the document. My desire is to replace all the " X |" with " |" to the end of that line.

  4. #4
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,170

    Re: Find a Last Occurrence of a String

    So there are multiple instances of " X |" on the last 'line' and you want to replace all of them with " |", not just the last occurrence?

    Can you attach a document to a post with some representative data (replace anything sensitive)? You do this via the paperclip symbol on the 'Go Advanced' tab.

  5. #5
    Forum Contributor
    Join Date
    06-17-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    157

    Re: Find a Last Occurrence of a String

    I have attached an example for you. It's a shorter version of the report I run, but it shows what I need it to show.

    Notice the string "BLOCK|" is located near the top of each page in the left column and the right column. Two lines below that is where I need the "X" replaced with spaces (and only for that line).

    On a side note, I'm using a font call Orator15 BT. I'm not sure how the document will look if the font type isn't installed.
    Attached Files Attached Files

  6. #6
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,170

    Re: Find a Last Occurrence of a String

    In your sample file, the first three sample paragraphs with "BLOCK|" don't have any " X |" strings in the 2nd paragraph below, though the third paragraph below does. Only the fourth paragraph with "BLOCK|" has any " X |" strings in the 2nd paragraph below. So which paragraphs are we working with? The following macro assumes it's the 2nd below:
    Please Login or Register  to view this content.
    To work with the 3rd paragraph below instead, add another *^13 to the Find expression.

  7. #7
    Forum Contributor
    Join Date
    06-17-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    157

    Re: Find a Last Occurrence of a String

    It does exactly what I want it to do. Thank you.

  8. #8
    Registered User
    Join Date
    10-28-2019
    Location
    Rochester, England
    MS-Off Ver
    365
    Posts
    1

    Re: Find a Last Occurrence of a String

    Hi, I have a similar issue. where I need to search an excel table for a string, located in in column (4), test a corresponding date in Column (17) is before a specific date and then if both are true, I then need to return the corresponding value from Column (1). Looping through each row in the table and generating a concatenated string of all the Column (1) values.

  9. #9
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Ipswich, England
    MS-Off Ver
    Excel 2019 (Win 10 - Work) & 365 Subscription (Win 10 - Home)
    Posts
    32,618

    Re: Find a Last Occurrence of a String

    Administrative Note:

    Welcome to the forum.

    We are happy to help, however whilst you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original.

    Please see Forum Rule #4 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!

    Forum Rules (updated September 2018): please read them here.
    How to use the Power Query code you've been given: help here. More about the Power suite here.
    Don't forget to say "thank you" to those who have helped you in your thread. If you wish, you can also reward them by clicking on their reputation star bottom left.

  10. #10
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,170

    Re: Find a Last Occurrence of a String

    Quote Originally Posted by Sampson_B View Post
    Hi, I have a similar issue. where I need to search an excel table for a string, located in in column (4), test a corresponding date in Column (17) is before a specific date and then if both are true, I then need to return the corresponding value from Column (1). Looping through each row in the table and generating a concatenated string of all the Column (1) values.
    Given that this thread concerns Word (it's even posted in the Word Programming forum) and your issue concerns a quite different task in Excel, the 'similarity' is tangential at best...

+ 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