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
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 &.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 286 |
Nodes: | 16 (2 / 14) |
Uptime: | 84:43:43 |
Calls: | 6,495 |
Calls today: | 6 |
Files: | 12,097 |
Messages: | 5,276,962 |