Look for some text, then count items, report, repeat

N

Novice

Hey all, I'm trying to write some VBA that will look for a series of
characters as defined in the first element in a String array. Then, to start
counting occurences of some other text, then when it encounters the next
element in the array, to report that total, and then repeat. For example, if
I define my array as this:
Dim sectionArray(15) As String
sectionArray(0) = ""
sectionArray(1) = "THIS is TEXT"
sectionArray(2) = "ThIS IS OtheR texT"
sectionArray(2) = "ThE TexT Is UnIMPORTANT"

And I'm looking to count occurences of the text "thesis" and "frog".

And I have the following text:
This is just an example THIS is TEXT - I want to put a few occurences of
thesis and another word frog. I think I'll throw an extra frog occurence
here. ThIS IS OtheR texT so now I'll throw.... some other frog text and also
a couple of thesis thesis occurences ThE TexT Is UnIMPORTANT

In the above I would like the MsgBox function to be invoked and report:
2 occurrence(s) of frog and 1 occurence(s) of thesis
Then to report:
1 occurrence(s) of frog and 2 occurence(s) of thesis

I have tried to do this - but I have encountered a few stumbling blocks:
1. I don't know how to declare a global array (which isn't necessary - but
I can make the code run faster if I can just declare the global array once)
2. I have a table that is using merged cells so there are only a limited
number of ways I can navigate the table - the way I'm currently using is
using the cell index (row and column).
3. I'm not certain of how to return my progress in the search outside of
returning the current cell I'm on - VBA doesn't seem to like the way I'm
trying to express of returning a Cell - should I just return the cell index
(row and column)?

-----------------------------------------
Function countOccurences (sectionArrayIndex As Integer) As Cell
Dim sectionArray(15) As String
sectionArray(0) = ""
sectionArray(1) = "THIS is TEXT"
sectionArray(2) = "ThIS IS OtheR texT"
sectionArray(2) = "ThE TexT Is UnIMPORTANT"

Dim numOfFrog As Integer
numOfFrog = 0
Dim numOfThesis As Integer
numOfThesis = 0
Dim currentCell As Cell
Dim arange As Range, i As Long, j As Long
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
Dim sectionArrayItem As String
sectionArrayItem = sectionArray(sectionArrayIndex)
With Selection.Find
Do While .Execute(FindText:=sectionArrayItem, Forward:=True,
Format:=True) = True
Set arange = Selection.Range
i = arange.Information(wdEndOfRangeRowNumber)
j = arange.Information(wdEndOfRangeColumnNumber)
If (i > 0) And (j > 0) Then
Set currentCell = Selection.Tables(1).Cell(i, j)
If (currentCell.Range.Text = "Frog") Then
numOfFrog = numOfFrog + 1
ElseIf (currentCell.Range.Text = "Frog") Then
numOfThesis = numOfThesis + 1
ElseIf (currentCell.Range.Text =
sectionArray(sectionArrayIndex+1)) Then
MsgBox ("Frogs: " & numOfFrog & " Thesis: " & numOfThesis)
currentCell
End If
.Parent.Move Unit:=wdCell, Count:=1
End If
Loop
End With
ActiveDocument.Tables(2).Cell(1,1)
End Function

Sub startMacro()
MsgBox (countOccurences(1).Range.Text)
End Sub
 
W

Word Heretic

G'day Novice,

1) First up, you can pass an array as a parameter by using ArrayName()

Second, if you change Dim to Public and stick it up the top of the
module, it is global. You set it on entry into your routine if it isnt
set.


2) Forget your method. Instead, declare a range and set the start of
the range to the end of the start text, and the end to the start of
the end text. Now you can MyRange.Find with wrap turned off and it
will stop at the end of the range for you. Every time you find one,
the range now has the found item. If you collapse the range and reset
the end to its orginal value, you can search on until the end.

3) The start of your range relative to the end will indicate progress.


Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


Novice reckoned:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top