for-loop

VBA, Office 2003

Page 1 of 1

5 Replies - 1017 Views - Last Post: 15 December 2009 - 04:46 AM Rate Topic: -----

#1 Rumo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-December 08

for-loop

Posted 14 December 2009 - 03:06 PM

Hi Guys!

My question concerns VBA, Office 2003. By using exit for i can quit a for-loop, but is there also a command to just skip an index? I've looked through some manuals, but couldn't find anything.

Something like


For i=1 to n

  if (i is prime) then
  skip i
  end if
 
  ...

Next index




Cheers, Rumo

This post has been edited by Rumo: 14 December 2009 - 03:59 PM

Is This A Good Question/Topic? 0
  • +

Replies To: for-loop

#2 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: for-loop

Posted 14 December 2009 - 04:41 PM

hello :)

you can always do it like so :
For i=1 to n

  if (i is prime) then
  skip i
  end if
 
  ...
if something then i=i+1
Next i



you can always give it +1 on some condition and there is a For i=1 to n Step 2

good luck
Was This Post Helpful? 0
  • +
  • -

#3 vb5prgrmr  Icon User is offline

  • D.I.C Lover

Reputation: 109
  • View blog
  • Posts: 1,016
  • Joined: 21-March 09

Re: for-loop

Posted 14 December 2009 - 10:59 PM

Dim ForLoopCounter As Integer
For ForLoopCounter = 1 To 13
  If ForLoopCounter Mod 3 = 0 Then
	'your skipping here
  Else
	'your working here
  End If
Next ForLoopCounter





Good Luck
Was This Post Helpful? 0
  • +
  • -

#4 Rumo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-December 08

Re: for-loop

Posted 15 December 2009 - 12:12 AM

Thx alot for your suggestions!

vb5prgrmr:
Yep, that's how i did it too, but (because of my limited programming abilities) this led to excessive nesting :blink:

NoBrain:
I will try this!

Again, thx alot to both of you.

Cheers,

Rumo
Was This Post Helpful? 0
  • +
  • -

#5 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: for-loop

Posted 15 December 2009 - 04:17 AM

hello excuse me i misread the question .
vb5prgrmr way is the right .
Dim ForLoopCounter As Integer
For ForLoopCounter = 1 To 13
  If ForLoopCounter Mod 3 <> 0 Then
	'it is not prime so you do what you want with it
  End If
'if it is prime it will go to next counter it will not enter the function for not prime numbers 
Next ForLoopCounter



good luck dude :)
Was This Post Helpful? 0
  • +
  • -

#6 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: for-loop

Posted 15 December 2009 - 04:46 AM

hello again :)
here is some snippet i find on prime numbers
http://www.dreaminco...snippet2941.htm
by modify it we can do this
Private Function PrimeNum(ByVal lngNumber As Long) As Boolean

For i = 2 To lngNumber - 1			  'Note : We are checking all numbers smaller then entered number except 1 and itself, which is , Obvious.

If lngNumber Mod i = 0 Then			 'If the number gets divided by a number less then it leaving no remider, i.e., gets perfectly divided.
PrimeNum = True				  ' BANG : The alarm is on
Else
PrimeNum=False
End If

Next i						  

End Sub



so now we know if it is prime or not just put it in your for cycle and do something like this
Dim ForLoopCounter As Integer
For ForLoopCounter = 1 To 13
If PrimeNum(ForLoopCounter) = False Then
'it is not prime so you do what you want
End If
'if it is prime it will just jump to next counter
Next ForLoopCounter



good luck dude :)

This post has been edited by NoBrain: 15 December 2009 - 04:47 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1