1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Word Macro Loop of 1) Find 2)Select line 3)Delete line 4)find.

Discussion in 'Business Applications' started by Smilylex, Apr 11, 2013.

Thread Status:
Not open for further replies.
Advertisement
  1. Smilylex

    Smilylex Thread Starter

    Joined:
    Apr 11, 2013
    Messages:
    24
    Hello

    I have a need to find a text of X in a document go to end of line select whole line and delete line and then find next X in document. With 3500 X Id like to loop till end of document. In 2010 my (pc at home) the find X part of a recorded Macro does not show up. In 2007 it shows up so I can see how it works at office. I just need to know how to loop, the above of instruction "find X, select whole line, delete, find next X" and loop this.

    Eg

    YYYY 1
    AHKJUSKL XX
    AGSJUJ ()*(IO XX
    ASJ
    L:JOPO
    AKLSJXX
    DADAD

    needs to look like

    YYYY 1
    ASJ
    L:JOPO
    DADAD

    using XX as the reference of which line to delete.



    Cheers
    Al
     
  2. XCubed

    XCubed

    Joined:
    Feb 21, 2013
    Messages:
    520
    try this on a copy of your file.

    Code:
    Sub Macro1()
    '
    Range("A1").Select
    Do
    On Error GoTo 999
        Cells.Find(What:="xx", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
            xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
            , SearchFormat:=False).EntireRow.Delete
            
    Loop
    999 MsgBox ("all occurrences have been deleted")
    End Sub
     
  3. Rollin_Again

    Rollin_Again

    Joined:
    Sep 4, 2003
    Messages:
    4,912
    The macro you posted is for Excel not Word like the original poster requested. The code below should work

    Code:
    Sub DeleteLines()
    
    Selection.HomeKey Unit:=wdStory
    Selection.Find.ClearFormatting
    Selection.Find.Text = "XX"
    Do While Selection.Find.Execute = True
    Selection.HomeKey Unit:=wdLine
    Selection.MoveEnd Unit:=wdLine
    Selection.Delete
    Loop
    End Sub



    Rollin
     
  4. XCubed

    XCubed

    Joined:
    Feb 21, 2013
    Messages:
    520
    missed it:eek:. I need to learn to read more carefully..
     
  5. Smilylex

    Smilylex Thread Starter

    Joined:
    Apr 11, 2013
    Messages:
    24
    Solved Many Thanks I'll post solution next week when back from Holidays.
     
  6. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1095837

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice