Results 1 to 9 of 9

Thread: Solved: How to count occurences of a string in a doc?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,476
    Location
    I came across this methodology. It will count strings, but also as part of words.
     
    Option Compare Text
    Sub CountOccurences()
        Dim MyDoc As String, txt As String, t As String
    MyDoc = ActiveDocument.Range.Text
    txt = InputBox("Text to find")
        t = Replace(MyDoc, txt, "")
        MsgBox (Len(MyDoc) - Len(t)) / Len(txt) & " occurrences of " & txt
    End Sub
    Last edited by Aussiebear; 03-19-2023 at 04:21 PM. Reason: Updated code tags
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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