View Single Post
  #9   Report Post  
Posted to microsoft.public.excel.programming
Floyd Floyd is offline
external usenet poster
 
Posts: 7
Default Excel Programming

Thank You Bob,

Please, help me in learning the new tools so that I can solve my problem.

"Bob Bridges" wrote:

Well, it looks a bit more complicated than necessary. Using arrays probably
isn't the VERY simplest way to do it anyway, but if you want to use arrays
for some reason I would do it this way:

Function EveryNth(Top As Long, Nth As Long)
Dim Fm(1 to Top) as Boolean 'Tracks which have been transferred
Dim To(1 to Top) as Long 'Numbers in new order (output of fnc)
Dim nTaken as Long 'Count of numbers tranferred to new
Dim N as Long 'Temp counter of Nth

' Initialize
For ii = 1 to Top
Fm(ii) = True
End If
iTo=0
nTaken = Top
N = Nth

' Loop until all numbers transferred to new list
Do
For iFm = 1 To Top
If Fm(iFm) Then 'this number not tranferred yet
N = N - 1 '...but we're transferring only every Nth
If N < 1 Then
iTo = iTo + 1
To(iTo) = iFm
Fm(iFm) = False 'mark as transferred
nTaken = nTaken - 1 'one down, nTaken to go
N = Nth 'restart the temp counter of Nth
End If
End If
Next iFm
Loop While nTaken 0 'keep going until all taken

End Function

Only slightly shorter, but it uses one fewer counter, I think, and the
counters are named for greater clarity.

But if want to keep on using the one you have, I think you'll have to tell
us what's "not working" about it. Is it bombing in the middle with an error
(and if so what error)? pretending to work but giving incorrect results (and
if so what results)?

Better yet, have you tried walking through its execution one step at a time,
using <F8 and the other debug tools? If you've never tried them before,
they're well worth learning; a little familiarity with them might well have
saved you these days of asking on the forum. If you're not sure how they
work, let us know and someone (me! pick me!) will help you get started.

--- "Floyd" wrote:
Thank You Bob and Scattered for your response but I looking for something
different. Maybe the information below is helpful.

I posted this question about two year ago and received a different answer.
The answer that I received had something to do with {=everynth(20, 3)}.
This formula represent 20 numbers (1-20) listd by every 3 numbers. The VBA
is listed below:

Option Explicit

Function EveryNth(Num As Long, Nth As Long)
Dim i As Long
Dim j As Long
Dim k As Long
Dim x As Variant
Dim y As Variant

ReDim x(1 To Num)
For i = 1 To Num
x(i) = i
Next i
ReDim y(1 To Num)

i = 0
j = 0
k = 0

Do
i = i + 1
If i Num Then i = 1

If x(i) < 0 Then
k = k + 1
If k = Nth Then
j = j + 1
y(j) = x(i)
If j = Num Then Exit Do
x(i) = 0
k = 0
End If
End If
Loop
EveryNth = y

End Function

This is the Macro that I want to use. Please review, it's just not working
for me.