Sunteți pe pagina 1din 7

Introduction to DOLOOP

Introduction
One of the first reason people develop macros is to eliminate manual repetitive tasks. When you want your VBA procedure to do something 10 times, hundreds of times and even thousands of times very rapidly you will use a loop like "DOLOOP". This workbook introduces you to the statement. When you start working with variables (lesson 19), you will be able to develop even more complex loops with "ForNext". You will discover how to work at the speed of light with large sets of data by sending the entire worksheet into a variable and working within it. Using this approach, a million calculations can be executed is less than 3 seconds. You can see the basic macro in the visual basic editor. You can also see the macro on the sheet "Code" with all the step by step explanations. Discover hundreds of macros in the 25 workbooks that are part of the tutorial on Excel macros that you can purchase and download from www.excel-vba.com. In the downloadable tutorial you will also find a complete version of lesson 20 presenting all the other VBA statements.

Send your comments to perte@excel-vba.com

LOOP

itive tasks. When s and even workbook

op even more d of light with large thin it. Using this

he macro on the

ial on Excel n the downloadable other VBA

VBA Code
Sub DeleteRowsZero() ' www.excel-vba.com ' Peter, 613-749-4695 Do Until Selection.Value = "" If Selection.Value = 0 Then Selection.EntireRow.Delete Else Selection.Offset(1, 0).Select End If Loop Range("A1").Select End Sub

The basic version of this macro includes 8 lines of code. Line 1: Do Until Selection.Value = "" This line is the first of a "Do" statement that ends with "Loop" 6 lines later. VBA will perform the action specified between the "Do" line and the "Loop" line until the value of the cell it ends up in is empty. Line 2: If Selection.Value = 0 Then This line is the first of an "If" statement that ends with "End If" 4 lines later. If the value of the selected cell is zero the macro will perform the action specified in the next line else it will perform the action specified after "Else". At the beginning of the macro the selected cell is the one selected by the user. After each loop the selected cell will be the next cell down because either the row of the selected cell will have been entirely deleted or the cell below will have been selected. Line 3: Selection.EntireRow.Delete If the condition specified on line 2 is met the action on line 3 will be executed and the macro will jump to line 6. The action required is to delete the entire row of the selected cell. Line 4: Else If the value of the selected cell at the beginning of the loop is not zero the macro will jump from line 2 to this line 4 Line 5: Selection.Offset(1, 0).Select If the value of the cell selected at the beginning of each loop is not zero the macro will perform the action specified in this line which is moving down one cell. Line 6: End If

Line 6: End If This is the end of the "If" statement and if you forget to write this line the visual basic editor will return an error message box when you try to run the macro.
Line 7: Loop Once the proper action has been executed the macro will go back to line 2

Line 8: Range("A1").Select After the macro has run all the way down the list of values the cursor is returned to the top of the sheet and cell "A1" is selected

VBA will perform the the cell it ends up in

If the value of the line else it will

ser. After each loop elected cell will have

ed and the macro will

macro will jump from

e macro will perform

sual basic editor will

urned to the top of

Enter a series of values in column "A" and run the macro by going to the menu bar "Tools/Macros", selecting "proDeleteRowsZero" and clicking "Run"

S-ar putea să vă placă și