Consulting

Results 1 to 2 of 2

Thread: Best Find/Replace

  1. #1
    VBAX Tutor jo15765's Avatar
    Joined
    Oct 2011
    Posts
    281
    Location

    Best Find/Replace

    I recorde a macro and this is the syntax it gave me...I know often the macro recorder will give you overly cumbersome code. Is there a way to condense/shorten this or is this truly what is needed? This is what the macro recorder gave me -- also I am going to be running it in conjunction with this code http://www.vbaexpress.com/kb/getarticle.php?kb_id=13 Thank you @Steiner but will be searching for 4 different variations. I.E. the example shows ID but it could be eID, or empID or employeeID. Thanks in advance!
    [vba]
    With Selection.Find
    .Text = "ID"
    .Replacement.Text = "Employee ID"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    [/vba]
    Go to Heaven for the climate, Hell for the company.
    ~~Mark Twain

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    The Find/replace code recorded by the macro recorder is actually one of the more efficient recorder products, but you've omitted the important first two lines! For what you've indicated, you could use either of:
    Sub Demo1()
        Application.ScreenUpdating = False
        With ActiveDocument.Range.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Forward = True
            .MatchCase = False
            .MatchWholeWord = True
            .MatchWildcards = False
            .Wrap = wdFindContinue
            .Replacement.Text = "Employee ID"
            .Text = "ID"
            .Execute Replace:=wdReplaceAll
            .Text = "eID"
            .Execute Replace:=wdReplaceAll
            .Text = "empID"
            .Execute Replace:=wdReplaceAll
            .Text = "employeeID"
            .Execute Replace:=wdReplaceAll
        End With
        Application.ScreenUpdating = True
    End Sub
    or
    Sub Demo2()
        Application.ScreenUpdating = False
        Dim StrFnd(), i As Long
        StrFnd() = Array("ID", "eID", "empID", "employeeID")
        With ActiveDocument.Range.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Forward = True
            .MatchCase = False
            .MatchWholeWord = True
            .MatchWildcards = False
            .Wrap = wdFindContinue
            .Replacement.Text = "Employee ID"
            For i = 0 To UBound(StrFnd)
                .Text = StrFnd(i)
                .Execute Replace:=wdReplaceAll
            Next
        End With
        Application.ScreenUpdating = True
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •