Cycle Through An Array

G

Greg Maxey

Hello,

I am working or a ever increasingly complicated macro for the sole purpose
of discovering how much I don't know. I strike a new vein with each
session.

The macro find text and highlights it. I define an array of five color
options. That part was easy because I hacked some code Doug Robbins posted.
The problem was how to start with yellow and then move to red, then to teal,
etc. Here is the fruit of hair pulling:

I set the variable value at 0 on opening the document and in another macro
when all highlighting is cleared. Then the IF statement changes it to a 1
effectively bypassing the 0 position in the array. Yellow highlight is
applied to the found text. Next time around the ELSEIF makes the 1 a 2 and
Red highlight is applied, etc.

Is this how this should be done, or am I driving a tack with a sledge
hammer?

On Error Resume Next
pCcnt = ActiveDocument.Variables("pCcnt").Value
On Error GoTo 0
If pCcnt = 0 Then
pCcnt = 1
ElseIf pCcnt = 1 Then pCcnt = 2
ElseIf pCcnt = 2 Then pCcnt = 3
ElseIf pCcnt = 3 Then pCcnt = 4
ElseIf pCcnt = 4 Then pCcnt = 5
ElseIf pCcnt = 5 Then pCcnt = 1
End If

ActiveDocument.Variables("pCcnt").Value = pCcnt

pCycleCols = Array(wdNoHighlight, wdYellow, wdRed, wdTeal, wdPink,
wdTurquoise)
 
J

Jason Eacott

at the very least you could simplify it greatly by using something like
this:

global pCcnt
global pCycleCols = Array(wdNoHighlight, wdYellow, wdRed, wdTeal,
wdPink,wdTurquoise)

public function getnextCol()
if (pCcnt <UBound(pCcnt)) then
pCcnt =pCcnt +1
else
pCcnt=0
end if
getnext=pCycleCols(pCcnt)
end function

or just make the thing a class and lose the globals.
 
D

DA

My guess is you'll get a few different suggestions
here :)

I would use a static and cycle like this:

---------------------
Sub CycleHighLight()
Static pCcnt As Long
Dim pCycleCols() As Variant
pCycleCols = Array(wdNoHighlight, wdYellow, wdRed, _
wdTeal, wdPink, wdTurquoise)

If pCcnt > 5 Then pCcnt = 0
Options.DefaultHighlightColorIndex = _
pCycleCols(pCcnt)
pCcnt = pCcnt + 1
End Sub
 
G

Greg Maxey

Jason, DA

Thank you both for your replies. Problem now is I have no idea how to apply
either of them. I have never heard of a Static and while I have heard the
term Class and Global, I have no idea how to apply any of them. The whole
purpose of the thing is to set pCcnt in the line below to 1 when the macro
is first run in an open document, then cycle through the array but never be
0.

Selection.Range.HighlightColorIndex = pCycleCols(pCcnt)

I will try tonight to figure out one or both of your suggested methods.
Would they be called somehow in the main macro?

Thanks again.
 
D

DA

Hi Greg

The code I provided is a self contained SUB-routine which
you can call from any other routine or just run by
itself.

If you run it a few times, you will see the highlighter
color cycle through on your main Word toolbar.

Best of luck,
Dennis
 
G

Greg Maxey

Dennis,

Thanks for your tips and explanation. I played around with your code and
the code Jason provided and applied it to my hairball of a macro. I have a
few more bugs to work out then I am going to post it here for the world to
see and tear apart :).
 

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