PDA

View Full Version : simple loop macro



asddsa88
02-26-2010, 07:56 AM
Hello, I just started programming in vba and I can't figure this out!

I have some instructions that must be repeated 15 times,
since I don't know ho to use a loop I just copied and pasted them 15 times and I got the job done... of course this is not a smart way to solve the problem,

so let's say the code to be repeated is #CODE#

how would you write the loop function?

do loop ...

#CODE#

end loop

any ideas? I know it's super simple,but I just can't do it!!

thanks

frubeng
02-26-2010, 08:39 AM
try:


for i=1 to 15
CODE
next i

Paul_Hossler
02-26-2010, 08:43 AM
In Help, check out the For/Next construct ---



Option Explicit ' always use this at the top
Sub LoopDemo()
Dim iCounter As Long


For iCounter = 1 To 10
MsgBox "Part 1 -- " & iCounter
Next iCounter

For iCounter = 10 To 4 Step -1
MsgBox "Part 2 -- " & iCounter
Next iCounter

For iCounter = 1 To 31 Step 5
MsgBox "Part 3 -- " & iCounter
Next iCounter

End Sub

asddsa88
02-27-2010, 07:57 AM
you rock guys!

problem solved! :beerchug: