• macro to position cusor using “find”

    Author
    Topic
    #505230

    I have a word (2013) document and I want to move the five characters that follow gtg to the beginning of the line. I have recorded a macro by clicking “find” which gives the navigation input box and I type “gtg, enter” this highlights gtg and then I close the Navigation box by clicking the “X”. I than do arrow right, “shift key+ arrow right 5 times. click copy. home, paste, space key, arrow down, home. The five characters are at the front of the first line and the cursor is now positioned at the front of the next line. This is what I want the macro to do but it does not record the clicks and key stroke for the “find” part. Following is the edit of the macro.

    Sub Macro3()

    ‘ Macro3 Macro


    CommandBars(“Navigation”).Visible = False
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.MoveRight Unit:=wdCharacter, Count:=5, Extend:=wdExtend
    Selection.Copy
    Selection.HomeKey Unit:=wdLine
    Selection.PasteAndFormat (wdFormatOriginalFormatting)
    Selection.TypeText Text:=” ”
    Selection.MoveDown Unit:=wdLine, Count:=1
    Selection.HomeKey Unit:=wdLine
    End Sub

    can some one give me VBA instructions that I can use to search for “gtg”? Also I would like to have this repeat to the end of the document.

    This is the first few lines of the document:

    a sower came from ancient hills (gtg 171).sbsong
    all with joyful exultation (gtg 798).sbsong
    amazing grace,how sweet the sound (gtg649).sbsong
    an upper room did our lord prepare (gtg 202).sbsong
    away in a manger (gtg 115) (ik).sbsong
    before the marvel of this night (gtg 125).sbsong

    Thanks

    Viewing 2 reply threads
    Author
    Replies
    • #1559575

      It seems to me you could do what you’re after via a wildcard Find/Replace, where:
      Find = [!^13]@gtg ([0-9]@))*^13
      Replace = 1. ^&
      No macros required (though you could record it as a macro). The above F/R will also work with numbers of any length following the gtg. The only line in your examples that might be a problem is the third, owing to a missing space between gtg and the number.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1559576

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1559577

      This is a simple way to achieve this:

      Code:
      Sub gtg()
          Dim x As Long
          
      again:
          Selection.Find.ClearFormatting
          With Selection.Find
              .Text = “gtg”
              .Forward = True
          End With
          Selection.Find.Execute
          Selection.Delete
          Selection.MoveLeft Unit:=wdCharacter, Count:=1
          Selection.MoveRight Unit:=wdCharacter, Count:=5, Extend:=wdExtend
          Selection.Cut
          Selection.HomeKey Unit:=wdLine
          Selection.Paste
          Selection.TypeText Text:=” ”
          x = Selection.movedown(Unit:=wdLine, Count:=1)
          If x = 0 Then
              Exit Sub
          End If
          GoTo again:
      
      End Sub
      
      • #1559621

        Thanks Richard, I will give this a try.
        Ivan

        This is a simple way to achieve this:

        Code:
        Sub gtg()
            Dim x As Long
            
        again:
            Selection.Find.ClearFormatting
            With Selection.Find
                .Text = “gtg”
                .Forward = True
            End With
            Selection.Find.Execute
            Selection.Delete
            Selection.MoveLeft Unit:=wdCharacter, Count:=1
            Selection.MoveRight Unit:=wdCharacter, Count:=5, Extend:=wdExtend
            Selection.Cut
            Selection.HomeKey Unit:=wdLine
            Selection.Paste
            Selection.TypeText Text:=” ”
            x = Selection.movedown(Unit:=wdLine, Count:=1)
            If x = 0 Then
                Exit Sub
            End If
            GoTo again:
        
        End Sub
        
    Viewing 2 reply threads
    Reply To: macro to position cusor using “find”

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: