loopig in Macro Programming

  • Thread starter Thread starter Rasoul Khoshravan Azar
  • Start date Start date
R

Rasoul Khoshravan Azar

In ordinary programming language like Basic we have a command called "loop"
which is used for recurring a command or a series of commands to reach a
criteria. How should it be done in Macro?
Specifically I want to cut and paste specified cells and then do it again
for another cells.
Any help is highly appreciated.
TIA, Rasoul
 
All MS Office programming is in the Visual Basic for Applications
language, which should give you an idea of how it ties in with Basic.
;-)

--
Regards,

Tushar Mehta, MS MVP -- Excel
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions
 
Rasoul,

You have a number of options

a For loop which uses a counter, such as

For i = 1 To 10
Cells(i,"A").Value = i
Next i

a For Each loop which uses an o bject, such as

i=1
For Each cell In Range ("A1:A10")
cell.Value = i
i = i + 1
Next cell

or a Do Loop

i = 1
Do
Cells(i,"A").Value = i
i = i + 1
Loop Until i > 10

Obviously the boundary criteria will change depending upon your requirement,
as will the actions.

There is also a While statement, but I don't use it but you may want to look
up in Help.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
From vba HELP index asking for LOOP
Do...Loop Statement
Repeats a block of statements while a condition is True or until a condition
becomes True.
Syntax

Do [{While | Until} condition]
[statements]
[Exit Do]
[statements]

Loop

Or, you can use this syntax:

Do
[statements]
[Exit Do]
[statements]

Loop [{While | Until} condition]

The Do Loop statement syntax has these parts:

Part Description
condition Optional. Numeric expression or string expression that is
True or False. If condition is Null, condition is treated as False.
statements One or more statements that are repeated while, or until,
condition is True.
 
Rasoul,

There are a variety of loop structures available in VBA. For
example: For/Next, For Each/Next, Do While, and Do Until. See
on line help for more details or post back with a more specific
question.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
Back
Top