none
VBA Find & Replace Multiple Word Case Changes Followed by LineBreaks? RRS feed

  • Question

  • I need a way to replace all instances of several names from lowercase to uppercase, for example:

    Mikey

    Where's my gear?

    JoKeR

    Umm, you'll have to guess.

    oracle

    It will be found when it is meant to be found.

    Johnny

    That means never.

    ---------------------

    And convert it to:

    MIKEY

    Where's my gear?

    JOKER

    Umm, you'll have to guess.

    ORACLE

    It will be found when it is meant to be found.

    JOHNNY

    That means never.

    -------------------------------

    And maybe there are 20 names, and the names get repeated over and over

    I do this  manually with Find & Replace, by searching for Mikey^l, replacing with MIKEY^l etc, for all 20 names

    the names are the same across many different "episodes" and I'd like to create a VBA script that just searches through the document and changes them all.. and if one particular name is not contained in a particular episode, it just skips it..

    Can anyone tell me how to write that?

    thanks

    Thursday, June 13, 2013 5:29 AM

Answers

  • Use

    Dim arrfind As Variant, arrreplace As Variant
    Dim i As Long
    Dim rngfound As Range
    arrfind = Split("Mikey|JoKeR|oracle|Johnny", "|") 'add the remaining names after Johnny, separating each with |
    arrreplace = Split("MIKEY|JOKER|ORACLE|JOHNNY", "|") "Ditto
    For i = LBound(arrfind) To UBound(arrfind)
        Selection.HomeKey wdStory
        Selection.ClearFormatting
        With Selection.Find
            Do While .Execute(FindText:=arrfind(i), Forward:=True, _
            MatchWildcards:=False, Wrap:=wdFindContinue, MatchCase:=True, MatchWholeWord:=True) = True
                Set rngfound = Selection.Range
                Selection.Collapse wdCollapseEnd
                Selection.MoveEnd wdCharacter, 1
                rngfound.Text = arrreplace(i)
            Loop
        End With
    Next i


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org

    Thursday, June 13, 2013 9:25 AM