• VBA Count String Occurances

    From David Kure@21:1/5 to All on Sun Jun 24 17:21:41 2018
    Hi!

    Extremely new to VBA and I came across this code in the forum that I would like to use to count specific instances of strings in the doc. It seemed pretty simple and straightforward.

    Essentially I want to be able to run the code to show me how many instances of some predefined strings occur in a message box.

    The issue I am having is that the code issues an error on line 22 and highlights all text in red with the message "expected expression". Any thoughts or ideas on what the issue could be? Than you in advance!


    https://groups.google.com/forum/#!topic/microsoft.public.word.vba.beginners/XHb4bmYwrCU

    Sub wordcount()
    Dim wrd As Range
    Dim var As Variant
    Dim searchlist()
    Dim numfound() As Integer
    Dim idx As Integer
    Dim strResults As String
    'Add as many words as you'd like to this list
    searchlist = Array("Red", "Green", "Blue")
    ReDim numfound(0 To UBound(searchlist))
    For Each wrd In ActiveDocument.Words
    idx = 0
    For Each var In searchlist
    If Trim(wrd.Text) = searchlist(idx) Then
    numfound(idx) = numfound(idx) + 1
    End If
    idx = idx + 1
    Next var
    Next wrd
    idx = 0
    For Each var In searchlist
    strResults = strResults & searchlist(idx) & " : " &
    numfound(idx) & vbCr
    idx = idx + 1
    Next var
    MsgBox strResults
    End Sub

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From depressed@21:1/5 to tgamekh@gmail.com on Sun Jun 24 21:18:22 2018
    On Sun, 24 Jun 2018 17:21:41 -0700 (PDT), David Kure
    <tgamekh@gmail.com> wrote:

    Hi!

    Extremely new to VBA and I came across this code in the forum that I would like to use to count specific instances of strings in the doc. It seemed pretty simple and straightforward.

    Essentially I want to be able to run the code to show me how many instances of some predefined strings occur in a message box.

    The issue I am having is that the code issues an error on line 22 and highlights all text in red with the message "expected expression". Any thoughts or ideas on what the issue could be? Than you in advance!


    https://groups.google.com/forum/#!topic/microsoft.public.word.vba.beginners/XHb4bmYwrCU

    Sub wordcount()
    Dim wrd As Range
    Dim var As Variant
    Dim searchlist()
    Dim numfound() As Integer
    Dim idx As Integer
    Dim strResults As String
    'Add as many words as you'd like to this list
    searchlist = Array("Red", "Green", "Blue")
    ReDim numfound(0 To UBound(searchlist))
    For Each wrd In ActiveDocument.Words
    idx = 0
    For Each var In searchlist
    If Trim(wrd.Text) = searchlist(idx) Then
    numfound(idx) = numfound(idx) + 1
    End If
    idx = idx + 1
    Next var
    Next wrd
    idx = 0
    For Each var In searchlist
    strResults = strResults & searchlist(idx) & " : " &
    numfound(idx) & vbCr
    idx = idx + 1
    Next var
    MsgBox strResults
    End Sub


    You need to join lines 22 and 23 into the one line they were meant to
    be.

    In the alternative place an underscore at the end of the line after
    the & so that the next line is read as a continuation of line 22.
    Somehow "numfound(idx) & vbCr" got split off and placed on the next
    line

    The error is telling you it is looking for an expresssion at the end
    of the line after the &.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Kure@21:1/5 to All on Mon Jun 25 19:19:09 2018
    Fixed, I owe ya. Thank you!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Kure@21:1/5 to depressed on Wed Jul 4 13:24:12 2018
    On Sunday, June 24, 2018 at 9:18:27 PM UTC-4, depressed wrote:
    On Sun, 24 Jun 2018 17:21:41 -0700 (PDT), David Kure
    <tgamekh@gmail.com> wrote:

    Hi!

    Extremely new to VBA and I came across this code in the forum that I would like to use to count specific instances of strings in the doc. It seemed pretty simple and straightforward.

    Essentially I want to be able to run the code to show me how many instances of some predefined strings occur in a message box.

    The issue I am having is that the code issues an error on line 22 and highlights all text in red with the message "expected expression". Any thoughts or ideas on what the issue could be? Than you in advance!


    https://groups.google.com/forum/#!topic/microsoft.public.word.vba.beginners/XHb4bmYwrCU

    Sub wordcount()
    Dim wrd As Range
    Dim var As Variant
    Dim searchlist()
    Dim numfound() As Integer
    Dim idx As Integer
    Dim strResults As String
    'Add as many words as you'd like to this list
    searchlist = Array("Red", "Green", "Blue")
    ReDim numfound(0 To UBound(searchlist))
    For Each wrd In ActiveDocument.Words
    idx = 0
    For Each var In searchlist
    If Trim(wrd.Text) = searchlist(idx) Then
    numfound(idx) = numfound(idx) + 1
    End If
    idx = idx + 1
    Next var
    Next wrd
    idx = 0
    For Each var In searchlist
    strResults = strResults & searchlist(idx) & " : " &
    numfound(idx) & vbCr
    idx = idx + 1
    Next var
    MsgBox strResults
    End Sub


    You need to join lines 22 and 23 into the one line they were meant to
    be.

    In the alternative place an underscore at the end of the line after
    the & so that the next line is read as a continuation of line 22.
    Somehow "numfound(idx) & vbCr" got split off and placed on the next
    line

    The error is telling you it is looking for an expresssion at the end
    of the line after the &.


    Quick follow-up:

    One of the words in my array contains a special character: "N/A"

    Is there a reason with the code that it would not find any instances of N/A in the document? I also tried changing the array item to ,"N" & chr(47) & "A", but with no luck. I also see that the macro has an issue with spaces in the search string.

    Thoughts?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)