Home > database >  Need help on making a VBA Word macros for searching words
Need help on making a VBA Word macros for searching words

Time:12-26

I'm trying to make a macros for Microsoft Word that will search specific words in the selected/highlighted text and the result would show how much the word is used throughout the highlighted selected area. I've wrote a macros, and it sorta works, but the total value of words shown is always calculated through the entire document, not the selected part. Any idea on how to fix this?

Sub CountWords()
'macros for counting specific words in the document 
'to count the number of a specified word, this word needs to be highlighted 
Dim rng As Range
Dim sWord As String
Dim i As Long
Dim sWord As String

Set rng = Selection.Range

Application.ScreenUpdating = False

    sWord = InputBox( _
        Prompt:="What word do you want to count?", _
        Title:="Count Words", Default:="")
   With rng.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = sWord
      .Forward = True
      .MatchWholeWord = True
      .MatchWildcards = False
      .Wrap = wdFindStop
      Do While .Execute
         i = i   1
      Loop
   End With
   Select Case i
      Case 2 To 4
         MsgBox "word " & Chr(171) & sWord & Chr(187) & " occurred in the document " & i & " times", _
            vbInformation, "word count"
      Case 1
         MsgBox "word " & Chr(171) & sWord & Chr(187) & " occurred in the document " & i & " times", _
            vbInformation, "word count"
      Case Else
         MsgBox "word " & Chr(171) & sWord & Chr(187) & " occurred in the document " & i & " times", _
            vbInformation, "word count"
   End Select
   rng.Find.Text = ""
Application.ScreenUpdating = True
End Sub

I've tried a bunch of stuff, even using other peoples codes, but every single one of em always counts specific words used throughout the *entire* doc, not selected/highlighted area. 

CodePudding user response:

For example:

Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range, sWord As String, i As Long
sWord = InputBox(Prompt:="What word do you want to count?", Title:="Count Words", Default:="")
With Selection
  Set Rng = .Range
  .Collapse wdCollapseStart
   With .Range
    With .Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = sWord
      .Forward = True
      .MatchWholeWord = True
      .MatchWildcards = False
      .Wrap = wdFindStop
    End With
    Do While .Find.Execute
      If .InRange(Rng) = False Then Exit Do
      i = i   1
    Loop
  End With
End With
Rng.Select
MsgBox "The word " & Chr(171) & sWord & Chr(187) & " occurred " & i & " times in the selected range.", _
  vbInformation, "word count"
Application.ScreenUpdating = True
End Sub

CodePudding user response:

Notwithstanding it being rather generic but somewhat it being a silver lining since I'm sure you know about your specific cases which doesn't add much to redo:

Sub CountWords()
      Dim w As String
      Dim ct As Integer
      Dim selection As Range
    
      word = InputBox("your word goes here")
      Set selection = Selection.Range
    'looping around here to spot how many times the word appears...
      count = 0
      With selection.Find
        .Text = w
        .MatchCase = True
        .MatchWholeWord = True
        Do While .Execute
          ct = ct   1
        Loop
      End With
    
      MsgBox ".... '" & w & "' .... " & ct & " # of times"
    End Sub
  • Related