Macro to Find Delete

When I try to create a macro to find "@@" and then delete "@@", I get a macro that does not work.

I choose record macro, select keyboard, key strokes are Ctl1,2, Go to Home, Find, Back space [to clear Find], type @@, Enter, close Find Box and either hit space bar then back space, or, back space only and then close record. What am I doing wrong? My goal is to enter text in various parts of a work document. I do this instead to mail merge.

lnb
Answer
Answer

Sounds like you've recorded a macro, but you haven't looked at the resulting code (otherwise you would have posted it here!).

 

In Word press Atl+F11 to open the Visual Basic Editor.  Try to find your macro there, probably in a module called NewMacros, either in your docuement or in the Normal template, look in the top left corner in the Project Explorer pane of the VBE (post back if you can't find it).

 

In the mean time, try recording your macro by using Replace.  Ctrl+H opens the Replace dialog, and you can replace "@@" with "" (nothing), then click Replace All.  The resulting macro code should look like this:

 

Sub Macro1()
'
' Macro1 Macro
'
'
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "@@"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub

 

but the macro recorder doesn't always record efficient code.  The above could be simplified to:

 

Sub Macro1()
    With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "@@"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=wdReplaceAll
    End With
End Sub
 

Hope that helps.

Cheers
Rich

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 742 Applies to: