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
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