+ Reply to Thread
Results 1 to 17 of 17

How to find a specific word and replace the immediate next word

  1. #1
    Forum Contributor
    Join Date
    11-29-2012
    Location
    Coimbatore, India
    MS-Off Ver
    Excel 2010
    Posts
    101

    How to find a specific word and replace the immediate next word

    Hi All,

    I'm searching for a particular word in a word document using a macro. Say for example i'm searching the word "Amount". I need to replace the very next word of the found word. Suppose there is a sentence like "Amount $5,000". I will search for the word "Amount" and have to replace the $5,000 with some other value.

    One can raise a doubt why need to search for "Amount" instead of "$5,000". I will tell the reason. $5,000 will not be same in all the documents. whereas "Amount" will be constant. So I'm invoking a search t find the word "Amount" and replace the very next word. I did some coding. Can you please let me know where should i have to correct.

    Please Login or Register  to view this content.

  2. #2
    Forum Contributor
    Join Date
    11-29-2012
    Location
    Coimbatore, India
    MS-Off Ver
    Excel 2010
    Posts
    101

    Re: How to find a specific word and replace the immediate next word

    Hi.. anyone got idea on this??

  3. #3
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: How to find a specific word and replace the immediate next word

    You can use a single find & replace instruction for the entire document using wildcards

    Please Login or Register  to view this content.
    If you like my contribution click the star icon!

  4. #4
    Forum Contributor
    Join Date
    11-29-2012
    Location
    Coimbatore, India
    MS-Off Ver
    Excel 2010
    Posts
    101

    Re: How to find a specific word and replace the immediate next word

    Quote Originally Posted by OllieB View Post
    You can use a single find & replace instruction for the entire document using wildcards

    Please Login or Register  to view this content.
    Thank You so much for your reply. Your idea works fine but for onething. Here in this place the word will be "Amount $?????". it means the word Amount will be BOLD always. so when i replace that word with "Amount $ 7,500" $7,500 also getting bold. I dont want it to be bold. Any suggestion??

  5. #5
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: How to find a specific word and replace the immediate next word

    Yes:
    - set the format on the first find & replace to normal font (to remove the BOLD);
    - run a second find & replace for the "Amount" string only and set format to BOLD

  6. #6
    Forum Contributor
    Join Date
    11-29-2012
    Location
    Coimbatore, India
    MS-Off Ver
    Excel 2010
    Posts
    101

    Re: How to find a specific word and replace the immediate next word

    Quote Originally Posted by OllieB View Post
    Yes:
    - set the format on the first find & replace to normal font (to remove the BOLD);
    - run a second find & replace for the "Amount" string only and set format to BOLD
    I did this after replacing the word. But unfortunately its not working.

    Please Login or Register  to view this content.
    am i missing something?

  7. #7
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: How to find a specific word and replace the immediate next word

    I just recorded a MACRO with the actions and it worked without a problem.

    Please see the attached code

    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    11-29-2012
    Location
    Coimbatore, India
    MS-Off Ver
    Excel 2010
    Posts
    101

    Re: How to find a specific word and replace the immediate next word

    Quote Originally Posted by OllieB View Post
    I just recorded a MACRO with the actions and it worked without a problem.

    Please see the attached code

    Please Login or Register  to view this content.
    when i use this part of code i'm getting wrong number of arguments or invalid property assignment error. can u please let me know what should i do. Ive used Option Explicit in the top
    Please Login or Register  to view this content.

  9. #9
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: How to find a specific word and replace the immediate next word

    The "Selection" part does not work because you are working from Excel and not from within Word. I recorded the macro in Word so you will need the selection variable to rng like you have done yourself in the first post.

    Please Login or Register  to view this content.

  10. #10
    Forum Contributor
    Join Date
    11-29-2012
    Location
    Coimbatore, India
    MS-Off Ver
    Excel 2010
    Posts
    101

    Re: How to find a specific word and replace the immediate next word

    Quote Originally Posted by OllieB View Post
    The "Selection" part does not work because you are working from Excel and not from within Word. I recorded the macro in Word so you will need the selection variable to rng like you have done yourself in the first post.

    Please Login or Register  to view this content.
    Thanks that's working great.
    I just faced one problem.

    "Amount $????????"
    Here the dollar value will not be always same. It will change everytime. So it can't be fixed. So is there anyother way where i can extract the next word of the Amount.

    I'm sorry to bug you all the time when i need a help.

  11. #11
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: How to find a specific word and replace the immediate next word

    I am not sure what you mean. The Find "Amount $????????" will find every occurrence of the string Amount $.... irrespective of the actual amount shown after the $, just like you asked.

  12. #12
    Forum Contributor
    Join Date
    11-29-2012
    Location
    Coimbatore, India
    MS-Off Ver
    Excel 2010
    Posts
    101

    Re: How to find a specific word and replace the immediate next word

    Quote Originally Posted by OllieB View Post
    I am not sure what you mean. The Find "Amount $????????" will find every occurrence of the string Amount $.... irrespective of the actual amount shown after the $, just like you asked.
    yes you are right.. Let me tell you clearly. I have these two sentences with bullets.
    • Amount $5,000
    • Payment $8,500

    so now "Amount $????????" replacing some part of next sentence as well.(ie its getting replaced as Amount $7,500yment $8,500). And also $5,000 can be $15,000 at times. so we can't fix the number of Question marks. This is my concern here. Hope i'm clear

  13. #13
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: How to find a specific word and replace the immediate next word

    If this is your problem, the automated find & replace solution will only work if you know the length of the amount string you are replacing. If you do not this up front, then the current solution we are discussing will not work and you are back at square one.

  14. #14
    Forum Contributor
    Join Date
    11-29-2012
    Location
    Coimbatore, India
    MS-Off Ver
    Excel 2010
    Posts
    101

    Re: How to find a specific word and replace the immediate next word

    Quote Originally Posted by OllieB View Post
    If this is your problem, the automated find & replace solution will only work if you know the length of the amount string you are replacing. If you do not this up front, then the current solution we are discussing will not work and you are back at square one.
    OOPS.. my bad.. I know this is too much to ask you...you got any idea for my problem?

  15. #15
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: How to find a specific word and replace the immediate next word

    A dirty way to do this, is to consider that the contents of the document can also be read/updated as a string

    Please Login or Register  to view this content.
    So if your documents are not too large you could use a regular search loop in VBA to find Amount and isolate and replace the $value. However if your documents are very large I do not know how VBA will handle the size of the string. Word is not my speciality.

  16. #16
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: How to find a specific word and replace the immediate next word

    For "small" document the following might work (replace new_amount with actual value provided as a parameter or something like that)

    Please Login or Register  to view this content.

  17. #17
    Forum Contributor
    Join Date
    11-29-2012
    Location
    Coimbatore, India
    MS-Off Ver
    Excel 2010
    Posts
    101

    Re: How to find a specific word and replace the immediate next word

    Quote Originally Posted by OllieB View Post
    For "small" document the following might work (replace new_amount with actual value provided as a parameter or something like that)

    Please Login or Register  to view this content.
    Thanks much for your help. I will try this.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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