View Full Version : [SOLVED:] Find Replace single letter on its own
pk247
02-06-2017, 02:27 AM
Hi All,
I hope someone can help please?  
I'd like to find the letter "P" (on it's own) in a document and remove it. So I think Find/Replace would work but I can't figure out how to get the letter on its own i.e. no text to the left and no text to the right of the letter "P" in upper case.
Any help would be appreciated.
Many thanks!
Paul, Ireland
:beerchug:
gmaxey
02-06-2017, 07:08 AM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Range
  If ActiveDocument.Paragraphs(1).Range.Characters(1).Text = "P" Then
    If ActiveDocument.Paragraphs(1).Range.Characters(2) Like _
                      "[" & Chr(13) + Chr(11) + Chr(32) + Chr(9) + Chr(160) & "]" Then
      ActiveDocument.Paragraphs(1).Range.Characters(1).Delete
     End If
  End If
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Text = "[" & Chr(13) + Chr(11) + Chr(32) + Chr(9) + Chr(160) & "]P" _
           & "[" & Chr(13) + Chr(11) + Chr(32) + Chr(9) + Chr(160) & "]"
    .MatchCase = True
    .MatchWildcards = True
    While .Execute
      oRng.Start = oRng.Start + 1
      oRng.End = oRng.End - 1
      oRng.Delete
      oRng.Collapse wdCollapseEnd
    Wend
  End With
lbl_Exit:
  Exit Sub
End Sub
pk247
02-06-2017, 10:49 AM
Thanks very much Greg!  And of course I left one vital requirement :( - the P's are in tables...  Nevertheless I understand what you've written and will try convert it.  
I figured there would have been some sort of way use Find/Replace with Wildcards but adding this to my list of macro buttons is just as good.
Thanks again!
Paul, Ireland
:beerchug:
gmaxey
02-06-2017, 02:29 PM
In that case perhaps:
Sub ScratchMacroII()
     'A basic Word macro coded by Greg Maxey
    Dim oRng As Range
    Set oRng = ActiveDocument.Range
    oRng.InsertBefore vbCr
    With oRng.Find
        .Text = "P"
        .MatchCase = True
        While .Execute
            If oRng.Information(wdWithInTable) Then
                If oRng.Characters.Last.Next Like _
                "[" & Chr(13) + Chr(11) + Chr(32) + Chr(9) + Chr(160) & "]" Or _
                Len(oRng.Characters.First.Previous) = 2 And _
                oRng.Characters.First.Previous Like _
                "[" & Chr(13) + Chr(11) + Chr(32) + Chr(9) + Chr(160) & "]" Or _
                Len(oRng.Characters.Last.Next) = 2 Then
                    oRng.Delete
                End If
                oRng.Collapse wdCollapseEnd
            End If
        Wend
    End With
    ActiveDocument.Paragraphs(1).Range.Delete
lbl_Exit:
    Exit Sub
End Sub
pk247
02-08-2017, 04:18 PM
Thank you so much Greg - that worked perfectly in my tables! I often do some very custom find replaces and this always stumped me so I did it manually. You just saved me 30 minutes a month,which in the grand scheme of things is great! 
Thanks again,
Paul, Ireland
:beerchug:
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.