+ Reply to Thread
Results 1 to 6 of 6

Sub for replacement: how to get feeback

  1. #1
    Registered User
    Join Date
    10-10-2018
    Location
    Greece, Athens
    MS-Off Ver
    2007
    Posts
    3

    Sub for replacement: how to get feeback

    Hello guys,

    I searched the Internet in the past and I found the following piece of code.

    Please Login or Register  to view this content.
    I do not understand this Range thing (despite my experience in VBA in Excel), however this code does work.

    I need an upgrade to that code, to convert the Sub (the procedure) into a Function that would return the number of replacements.

    This code has proved to be excellent in replacing two consecutive spaces with one but ... ... here comes the big Bee Yu Tee. What about having three consecutive spaces or four?

    I could make a loop and run it many times e.g. 12 times. It would be rather unlikely to have more than 12 consecutive spaces in a document. But that sounds stupid and also increases the time required.


    If I could get the number of replacement I could run it in a repeat-until loop and run it until the number of replacements becomes zero.

    Any idea?

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: 2010, 2016
    Posts
    6,341

    Re: Sub for replacement: how to get feeback

    I'd not change it into function, but built in looping into the procedure.
    find.Execute returns true if it finds searched term

    so we shall loop until nothing is found.

    First I wrote it as:


    Please Login or Register  to view this content.
    But then thought that looping inside each story will be quicker - so try also on a large document this possibly faster version:
    Please Login or Register  to view this content.
    Best Regards,

    Kaper

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

    Re: Sub for replacement: how to get feeback

    Try:
    Please Login or Register  to view this content.
    Note that the above code processes both ordinary spaces and non-breaking spaces. However many spaces are replaced at a given location are treated as a single replacement.
    Cheers,
    Paul Edstein
    [MS MVP - Word]

  4. #4
    Registered User
    Join Date
    10-10-2018
    Location
    Greece, Athens
    MS-Off Ver
    2007
    Posts
    3

    Re: Sub for replacement: how to get feeback

    Quote Originally Posted by Kaper View Post
    But then thought that looping inside each story will be quicker - so try also on a large document this possibly faster version:
    Please Login or Register  to view this content.
    It works, thank you very much.

  5. #5
    Registered User
    Join Date
    10-10-2018
    Location
    Greece, Athens
    MS-Off Ver
    2007
    Posts
    3

    Re: Sub for replacement: how to get feeback

    Quote Originally Posted by macropod View Post
    Try:
    Please Login or Register  to view this content.
    Note that the above code processes both ordinary spaces and non-breaking spaces. However many spaces are replaced at a given location are treated as a single replacement.
    I got an erroe message.


    Run-time error '5560';

    The Find What text contains a Pattern Match expression which is not valid.

    PS: thanks for your help anyway.

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

    Re: Sub for replacement: how to get feeback

    Quote Originally Posted by Archangelos View Post
    I got an erroe message.


    Run-time error '5560';

    The Find What text contains a Pattern Match expression which is not valid.
    That indicates you're using a system with non-English-language regional settings. Change:
    .Text = "[ ^s]{2,}"
    to:
    .Text = "[ ^s]{2;}"

+ 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