#1
|
|||
|
|||
How to repeat a search followed by editing text?
I used the record macro function to record myself doing Ctrl + h to search for "[a-z]/[a-z]" (a / that is preceded and followed by any alphabetic character) and then used the arrow keys to move the cursor and insert spaces either side of the /, without overwriting the alphabetic characters that become highlighted in the search.
What I'm struggling with is how to repeat this action for the rest of the document? I want to search, then edit text, then search again, etc, until the end of the document. The search function alone just does the search, and if I use the search and replace function, because the alphabetic characters are highlighted in the search, replacement text overwrites those characters, which I don't want. I feel like there's some basic loop command or something but I can't find any examples of code that are similar enough to what I want, and I've tried and tested a few different approaches with no luck. Thanks. |
#2
|
|||
|
|||
Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey Dim oRng As Range Set oRng = ActiveDocument.Range With oRng.Find .Text = "([a-z])(/)([a-z])" .MatchWildcards = True .Replacement.Text = "\1 \2 \3" .Execute Replace:=wdReplaceAll End With lbl_Exit: Exit Sub End Sub |
#3
|
|||
|
|||
lbl_Exit:
Exit Sub What does this part mean? |
#4
|
|||
|
|||
It is just style and preference. I prefer not to hit End Sub so I always Exit before.
Exit Sub lblExit: is just a lable e.g., I might have use GoTo lbl_Exit: or Resume lbl_Exit: is some procedures. |
#5
|
|||
|
|||
Oh my goodness that worked perfectly! But I have no idea how lol
Thank you! |
#6
|
|||
|
|||
Adding the ( ) around the three elements of the find text term defines them as groups
So in the replace with term you are replacing with the first group of found characters, a space, the second group (your /), a space and finally the third group of found characters. |
#7
|
|||
|
|||
What does this part mean/do?
" .Replacement.Text = "\1 \2 \3" " |
#8
|
|||
|
|||
Replace the found text with group 1 "\1" "a space" group 2 "\2" "another space" and finally group 3 "\3"
|
#9
|
|||
|
|||
Is there a reason this wouldn't work in another macro which also contains "Dim char As Range"?
|
#10
|
||||
|
||||
Yes, it doesn't work in your other macro because you have no idea on what you are doing.
If you need help to diagnose why a macro doesn't work, you need to provide the problematic code and give us an idea on what you think the macro should be doing for you. 'Dim char as Range' won't cause a macro to fail unless you have already declared char.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#11
|
|||
|
|||
Well then I guess you're right and I do "have no idea on what" I am doing, but maybe that's why I'm here?
I have another macro that does a bunch of stuff, and the Dim char As Range goes towards formatting tables. It's over 500 lines of code and on another PC, so even if I knew how to provide it here, not knowing what could be causing the problem at all and just posting the entire macro seems silly. When I added gmaxey's code into the beginning of my other macro, it just skipped it altogether, no error messages or anything broken. The only thing I could think was that maybe having 2 x Dim something As Range might cause a problem, but I didn't think it would. |
#12
|
|||
|
|||
You can have as many range variables as you want.
Dim oRng as Range Dim oChar as Range Dim oWord as Range Dim oOpenRange As Range If your insert this code in another macro then it should run the same as it did on its own: Code:
Dim oRng As Range Set oRng = ActiveDocument.Range With oRng.Find .Text = "([a-z])(/)([a-z])" .MatchWildcards = True .Replacement.Text = "\1 \2 \3" .Execute Replace:=wdReplaceAll End With |
#13
|
||||
|
||||
Since Greg's macro works by itself, you can choose an alternative approach to including it in your bigger macro - by using a pointer rather than the actual macro content.
Put Greg's macro in the same module as the larger macro. When you want Greg's macro to run, add a line in the bigger macro with the name of Greg's macro eg Code:
Sub MyBigMacro() MsgBox "Working steps" ScratchMacro MsgBox "More working steps" End Sub Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey Dim oRng As Range Set oRng = ActiveDocument.Range With oRng.Find .Text = "([a-z])(/)([a-z])" .MatchWildcards = True .Replacement.Text = "\1 \2 \3" .Execute Replace:=wdReplaceAll End With lbl_Exit: Exit Sub End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
possible to repeat text with numbered list? | Burt | Word | 5 | 07-14-2019 04:02 PM |
outlook search tool locked with key word, it will not allow deleting or editing | roni | Outlook | 0 | 07-12-2017 07:31 AM |
Repeat text | dgame27 | Word | 1 | 07-06-2016 09:14 PM |
Automatically Repeat Text in Footer | Luxanais | Word | 2 | 09-24-2015 10:10 AM |
Text will repeat in the fields | ypurcaro | Word | 1 | 02-07-2013 01:46 AM |