+ Reply to Thread
Results 1 to 12 of 12

Sub for replacement: how to get feeback

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

    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: Office 365 in Windows environment
    Posts
    8,613

    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
    3,726

    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
    [Fmr MS MVP - Word]

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

    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
    10

    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
    3,726

    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;}"

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

    Re: Sub for replacement: how to get feeback

    Hello again,

    I have another question.

    The code includes the following structure.

    Please Login or Register  to view this content.
    I suppose that a word document consists of ... Stories? Is it right?

    The code above passes through all Stories and does the replacement.

    Is it possible to alter the code? Insted of doing replacements across the whole document to do them to a part of the document I have selected.


    Is anything like the following?

    Please Login or Register  to view this content.

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

    Re: Sub for replacement: how to get feeback

    To limit execution to a selected range, you'd use code like:
    Please Login or Register  to view this content.

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

    Re: Sub for replacement: how to get feeback

    Yeeaaaaah!!!

    This is what I needed. You see, I have a MS Word document in which I copy every day's reports (three per day). Each file has a full year's report. EVery time I run my code it passes through the whole document. I just need to have it done for the last days, not the all days.

    Thanks buddy.

  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,726

    Re: Sub for replacement: how to get feeback

    Well, if you copied the reports into a separate document first, then ran the original macro against that, you wouldn't need to ensure any particular range was selected. You could then copy & paste the edited result into the main document.

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

    Re: Sub for replacement: how to get feeback

    I had thought of that but I rejected the idea. It seemed a little bit counter productive.

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

    Re: Sub for replacement: how to get feeback

    macropod,

    thanks for your code.

    I worked on your code and the first work was successful. I had to alter the code a little in order to fit my needs (although further work is required).

    Please Login or Register  to view this content.
    One question, what is this "Application.ScreenUpdating" thing?

+ 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. If / Then Replacement
    By dsg0110 in forum Excel General
    Replies: 1
    Last Post: 07-18-2012, 10:43 PM
  2. Row() replacement...
    By vanguru in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 04-15-2009, 10:40 PM
  3. Replacement
    By T De Villiers in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-21-2006, 10:52 AM
  4. Replacement
    By mowen in forum Excel General
    Replies: 1
    Last Post: 09-07-2005, 04:05 PM
  5. Replacement
    By rasik in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 01-07-2005, 08:24 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