Consulting

Results 1 to 5 of 5

Thread: Find Replace single letter on its own

  1. #1
    VBAX Regular pk247's Avatar
    Joined
    Feb 2014
    Posts
    64
    Location

    Find Replace single letter on its own

    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


  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,118
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular pk247's Avatar
    Joined
    Feb 2014
    Posts
    64
    Location
    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


  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,118
    Location
    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
    Last edited by gmaxey; 02-06-2017 at 03:11 PM.
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    VBAX Regular pk247's Avatar
    Joined
    Feb 2014
    Posts
    64
    Location
    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •