word 2010 VBA macro question - Search for word, extend selection, then delete

brothertruffle880
brothertruffle880 used Ask the Experts™
on
I would like to have a macro that:
1.  Searches for the character string FIG.
2.  I would like to select the character string FIG and continue selecting characters to the right until the hard return character is reached.
3.  I would like to move backwards one character because I do  NOT want to select the hard return character. I'm just using the hard return character as a marker for how long I would like my selection to be.
4. I would like to pause and have a pop up message appearing that says "erase?"
5. I would like to delete the selected character string if I click the "yes" button. And I'd like to terminate the macro if I click the "no" button.
I do not need to loop or do until, etc.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Retired
Top Expert 2012
Commented:
This should do that
Sub DelFig()
    Dim rng As Range
    Set rng = ActiveDocument.Range
    With rng.Find
        .MatchWildcards = True
        .Text = "FIG*^13"
        If .Execute() Then
            If MsgBox("Delete?", vbYesNo) Then
                rng.MoveEnd wdCharacter, -1
                'rng.Select 'If you need to see what is being deleted, remove the first commenting apostrophe from this line
                rng.Delete
            End If
        End If
    End With
End Sub

Open in new window

Author

Commented:
GrahamSkan:
Wonderful solution. Thanks!
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and Programming
Top Expert 2015

Commented:
cool code, Graham ~ if one did want to loop, what would need to change? thanks
GrahamSkanRetired
Top Expert 2012

Commented:
It gets a bit more complicated, but the Find.Text remains the same. I've added a report message at the end.
Sub DelFigs()
    Dim rng As Range
    Dim f As Integer
    Dim d As Integer

    Set rng = ActiveDocument.Range
    Do While True
        With rng.Find
            .MatchWildcards = True
            .Text = "FIG*^13"
            If .Execute() Then
                f = f + 1
                rng.MoveEnd wdCharacter, -1
                rng.Select 'show text to be deleted
                Select Case MsgBox("Delete?", vbYesNoCancel)
                    Case vbYes
                        rng.Delete
                        d = d + 1
                    Case vbNo
                    Case vbCancel
                        Exit Do
                End Select
                'start from current position to avoid refinding non-deleted text
                rng.Start = rng.End
                rng.End = ActiveDocument.Range.End
            Else
                Exit Do
            End If
        End With
    Loop
    MsgBox "Finished. Found Count: " & f & ". Deleted count: " & d
End Sub

Open in new window

crystal (strive4peace) - Microsoft MVP, AccessRemote Training and Programming
Top Expert 2015

Commented:
thank you, Graham. My purpose is to color comments in pasted code green. Here is the final procedure:
Sub ColorCommentsAllGreen()
'170415 based on code by Graham Skan
'modified by crystal

   On Error GoTo Proc_Err

   Dim nCount As Long
   
   Dim rng As Range
   
   nCount = 0
   Set rng = ActiveDocument.Range
    
   Do While True
      With rng.Find
         .MatchWildcards = True
         .Text = "'*^13"
        If .Execute() Then
            rng.Font.Color = 26112 'dark Green
            nCount = nCount + 1
            'start from current position to avoid refinding text
            rng.Start = rng.End
            rng.End = ActiveDocument.Range.End
        Else
           Exit Do
        End If
      End With
   Loop
   MsgBox "Colored " & Format(nCount, "#,##0") & " comments", , "Done"
   
Proc_Exit:
   On Error Resume Next
   Set rng = Nothing
   Exit Sub
  
Proc_Err:
   MsgBox Err.Description, , _
        "ERROR " & Err.Number _
        & "   ColorCommentsAllGreen"

   Resume Proc_Exit
   Resume
End Sub

Open in new window

obviously this isn't perfect since I am not checking for a single quote in the code, just assuming it is the start of a comment, which it usually is ...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial