VBA: conditional text replacement

Important Notice

Please note that on 14th December 2023 users will experience an expected outage whilst we make upgrades to our network. We anticipate this process may take a couple of hours and so we apologise in advance for any inconvenience.

  • Hi BrainBoxes,

    I'm working in Ms Word.

    I'm using a straight forward search and replace macro,
    But my twist is that when the search finds a bracket '(' I would like the search to skip to the next line, leaving any text contained in brackets alone.

    Any ideas,

    Cheers in advance.

  • Hi,

    I'm not sure how much help I can be, nor how much you'll get in general on OzGrid (which focuses on Excel -- in the case of this question forum on Excel as it interfaces with Word). It sounds like your question is geared toward Word only.

    Be that as it may, I am not quite sure what you want done. "Next Line" in Word is a very ambiguous term. The next line as printed or displayed on the screen is not necessarily a condition that you can use to move around in Word with precision. Next Paragraph might work, or next line feed, or other searchable character.

    Also, if you are doing a "straight forward search and replace" and are finding a "(", why isn't it part of your replace? Are you saying that if a "(" occurs within a certain number of characters of the string you find then you want to treat if differently than otherwise?

    Again, I'm not sure I can provide much help, but clarifying your question a bit may help you get the answer you need. An exampel or two would probably be worth many paragraphs of explanation.

  • I'm sorry for posting this question here,
    But if we may carry on,
    This example may explain better.



    wants to read:-



  • If you are trying to place a hyphen before each opening parens, the search for "(" and repalce with "-(" If the search field (and the more specific the better) is "(TU", then your replace field would be "-(TU"

    NB: the quotation marks do not actually get entered into the Word search dialog box.

    Does this help or get closer?

  • Thomach = TDQ again. :yes:

    No need to apologize for the post, the OzGrid Excel Forum page does say ". . . or Word only" on it in the Excel/Word section. It's just that the answers for non Excel issues sometimes take longer.:wink2:

    If all of your search and replace situations are like this then can't you search for "U(" and replace with "U-("? This would avoid the unwanted result you would get from searching for "U" and replacing with "U-".


  • Looking more closely at your sample, I think you are asking how to distinguish a search for say "MRS" when it is in a sentence MRS ROBINSON SAID ... versus inside parens: (SAID MRS ROBINSON). I'm not sure this is possible if there is nothing else to distinguish between the two cases. There are special search characters e.g. < and &gt; placed before or after your string find results only if they are at the start of end of a word. The * will allow breaks of any length, but a search for MRS*) will find either of the above if there is a close parens anywhere later in the text (even if a open parens comes first).

    Anyway, I'm afraid I do not see how to do that one.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!