VBA brush up 06: repeating actions in VBA

Source: Internet
Author: User
Technorati labels: VBA, loop, for, while, do

From: julitta korol, "access.2003.programming. By. example. With. VBA. xml. And. asp", by wordware Publishing, Inc. 2005, p91-p101

(1) using the do... While Loop

  1. Do While Condition
  2. Statement1
  3. Statement2
  4. Statementn
  5. Loop

(2) continuously display an input box until the user enters the correct password

  1. Do While pword <> "dada"
  2. Pword = inputbox ("what is the report password? ")
  3. Loop

(3) Another approach to the do... While Loop

  1. Do
  2. Statement1
  3. Statement2
  4. Statementn
  5. Loop while Condition

When you test the condition at the bottom of the loop, the statements inside the loop areExecuted at least once.

(4) infinite loops:

  1. Sub sayhello ()
  2. Do
  3. Msgbox "hello ."
  4. Loop
  5. End sub

To stop the execution of the infinite loop, you must pressCTRL + break. When Visual Basic displays the message box "code execution has been interrupted," click end to end the procedure.

(5) do... Until repeats a block of code as long as something is false.

  1. Do until Condition
  2. Statement1
  3. Statement2
  4. Statementn
  5. Loop

(6)

  1. Do
  2. Statement1
  3. Statement2
  4. Statementn
  5. Loop until Condition

If you want the statementsExecute at least once, No matter what the value of the condition, place the condition on the line with the loop statement.

(7)

  1. For counter = start to end [step increment]
  2. Statement1
  3. Statement2
  4. Statementn
  5. Next [Counter]

(8)

  1. For each element in group
  2. Statement1
  3. Statement2
  4. Statementn
  5. Next [element]

Element is a variable to which all the elements of an array or collection will be assigned. This variable has to be ofVariant data type for an array and of the object data type for a collection. Group is a name of a collection or an array.

(9) Exiting loops early:Exit for, exit do, exit sub, exit function.

(10) When writing nested loops, you must make sure that each inner loop is completely contained inside the outer loop. Also, each loop has to have a unique counter variable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.