A family of Microsoft word processing software products for creating web, email, and print documents.
Hi Steve8d,
It’s always my Pleasure to assist you, Sorry for the inconvenience caused It looks like the issue is with the Selection.Find.Text = "(by" line in your code. The Find method searches for a specified text string within the selected range, and the Text property sets or returns the text to be found.
In this case, it appears that the Text property is being set to "(by", which means that the Find method will search for this exact text string. However, it seems that the text in your document is not exactly "(by", but is instead something like "(by Author)". This means that the Find method will not find this text, and will instead move on to the next Find statement.
To fix this issue, you can modify the Text property to search for a more general pattern that will match the text in your document. For example, you could use the following regular expression to search for text that begins with "(by" and ends with ")":
. Text = "(by.*)$"
This regular expression will match any text that begins with "(by" and ends with ")", and will allow the Find` method to find the text in your document.
Additionally, it looks like there may be some unnecessary or redundant code in your script. For example, the Selection.Find.Execute method is called multiple times within the Do While loop, but this is not necessary because the Execute method is already called within the Do While condition. You may want to review your code and remove any unnecessary or redundant statements to improve its efficiency.
To remove unnecessary or redundant statements and improve the efficiency of your code, you can follow these steps:
- Review your code carefully and identify any statements or blocks of code that do not appear to be necessary for the desired outcome of your script.
- Remove these unnecessary or redundant statements from your code.
- Test your code to ensure that it still produces the desired results.
- Repeat these steps as needed until your code is efficient and free of unnecessary or redundant statements.
It's important to note that the specific steps for removing unnecessary or redundant statements will depend on the specific code you are working with. However, some general principles to keep in mind when trying to improve the efficiency of your code include:
- Avoid repeating the same operations or calculations multiple times within a single script. Instead, try to write your code in a way that allows you to perform the operation or calculation once and then reuse the result later in the script.
- Avoid using long, complex conditional statements or nested loops if possible. Instead, try to break your code into smaller, simpler pieces that can be combined to achieve the desired result.
- Avoid using multiple, similar methods or functions to perform the same operation. Instead, try to find a single method or function that can be used to accomplish the task in a more efficient way.
- Avoid using large, complex data structures if possible. Instead, try to use simpler data structures that are easier to work with and can be processed more efficiently by your code.
By following these principles, you can improve the efficiency of your code and make it easier to maintain and update in the future.
Here's the revised version of your Code :-
Sub The_Second_Tidy_up() Dim rng As Range
' Find text that matches "(by" and delete it With Selection.Find . Text = "(by" . Replacement.Text = "" . Forward = True . Wrap = wdFindStop . Format = True . MatchCase = True . MatchWholeWord = False . MatchWildcards = False . MatchSoundsLike = False . MatchAllWordForms = False
Do While Selection.Find.Execute And Selection.Find.Text = "(by" Selection.Extend Selection.Find.Execute Selection.Delete Loop End With
' Find text that matches ")" and delete it With Selection.Find . Text = ")" . Replacement.Text = "" . Forward = True . Wrap = wdFindStop . Format = True . MatchCase = True . MatchWholeWord = False . MatchWildcards = False . MatchSoundsLike = False . MatchAllWordForms = False
Do While Selection.Find.Execute And Selection.Find.Text = ")" Selection.Extend Selection.Find.Execute Selection.Delete Loop End With
It's too long rest part can be done by you from "Find text that matches a name followed by a number in parentheses, and delete it"
If you have any other questions or need assistance with anything, please don't hesitate to let me know. I'm here to help to the best of my ability.
Give back to the Community. Help the next person who has this issue by indicating if this reply solved your problem. Click Yes or No below.
Best Regards, Sneha