Share via

Using VBA to select text.

SteveD 435 Reputation points
2026-04-20T21:09:48.78+00:00

Hello from Steve

Is this possible please to select text 5PBPX using VBA


Mod moved from : Microsoft 365 and Office | Word | For home | Windows

Developer technologies | Visual Basic for Applications
0 comments No comments

Answer accepted by question author

  1. Nancy Vo (WICLOUD CORPORATION) 4,190 Reputation points Microsoft External Staff Moderator
    2026-04-22T03:38:38.0366667+00:00

    Hello @SteveD ,

    Thanks for your question.

    Yes, it's absolutely possible. You can refer to these following code examples:

    • In Microsoft Word:
    Sub SelectText()
        Dim oRange As Range
    
        With Selection.Find
            .Text = "5PBPX"
            .Execute
        End With
    
        If Selection.Find.Found Then
            MsgBox "Text '5PBPX' found and selected!"
        Else
            MsgBox "Text not found."
        End If
    End Sub
    
    • In Microsoft Excel, when you want to select a cell containing "5PBPX":
    Sub SelectCellWithText()
        Dim cell As Range
    
        Set cell = ActiveSheet.Cells.Find(What:="5PBPX", LookIn:=xlValues, LookAt:=xlWhole)
    
        If Not cell Is Nothing Then
            cell.Select
            MsgBox "Cell " & cell.Address & " containing '5PBPX' is selected!"
        Else
            MsgBox "Text '5PBPX' not found."
        End If
    End Sub
    

    I hope this was helpful to you. If so, I’d appreciate it if you could provide feedback using the guidance.

    Was this answer helpful?

    1 person found this answer helpful.

Answer accepted by question author

  1. Q&A Assist
    2026-04-20T21:09:58.1866667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    Yes. Use the Find object (optionally with Selection or a Range) to locate and select the exact text 5PBPX in the document.

    Example 1 – select the first occurrence of 5PBPX in the whole document:

    Sub SelectText5PBPX()
        With ActiveDocument.Content.Find
            .ClearFormatting
            .Text = "5PBPX"
            .Replacement.ClearFormatting
            If .Execute(FindText:="5PBPX", Forward:=True, _
                        Wrap:=wdFindStop, Format:=False) Then
                ' The Find from a Range leaves the match as the current Selection
                ' so the text 5PBPX is now selected.
            End If
        End With
    End Sub
    

    Example 2 – select the next occurrence of 5PBPX starting from the current selection:

    Sub SelectNext5PBPXFromSelection()
        With Selection.Find
            .ClearFormatting
            .Text = "5PBPX"
            .Replacement.ClearFormatting
            .Execute FindText:="5PBPX", Forward:=True, _
                     Wrap:=wdFindStop, Format:=False
            ' If found, 5PBPX is now selected.
        End With
    End Sub
    

    Once selected, any Selection-based code can operate on that text (for example, formatting or replacing it).


    References:

    Was this answer helpful?

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.