Consulting

Results 1 to 2 of 2

Thread: Replacing text with Regular expressions

  1. #1
    VBAX Regular
    Joined
    Oct 2015
    Posts
    13
    Location

    Replacing text with Regular expressions

    What I'm trying to accomplish here is to replace all speaker identifications from the text below (F6:, F7:, F8:, F9

    F6: I just I don't,
    F7: think that there is a
    F8: basis in the law for that argument.
    F7: And so our position is the same that

    So what the user will do is they will highlight the entirety of the text shown above and want to remove all the speaker identifications F#: Code is below:
        Dim oRng As Range
        Dim regExp As Object
        Dim startingplace As Range
        Set regExp = CreateObject("vbscript.regexp")
        
        Application.ScreenUpdating = False
    
    
        Set oRng = Selection.Range
        oRng.Select
        
        Debug.Print Len(Selection.Text)
        If Len(Selection.Text) > 1 Then
        
            With regExp
                .MultiLine = True
                .Pattern = "[A-Za-z0-9\s\.]*\:\s\s"
                .Global = True
                'Selection.Text = .Replace(oRng.Text, "")
            End With
        End If
    End Result is:

    "I just I don't,And so our position is the same that"

    So it did the first and the last ID but missed the two middle ones and completely remove those entire lines.

    Thanks in advance for any help or direction on this.

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,313
    Location
    All you need for that is a wildcard Find/Replace, with:
    Find = F[0-9]@:(*)^13
    Replace = \1
    You don't even need a macro!
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

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