Microsoft Excel Macros


VBA Lesson 20: VBA for Excel Statements

Among the VBA statements yu will discover If, Then, ElseIf, End If, Do, Loop, For, Next, While, With, the powerful "Select Case" statement and others.

A lot of visitors ask us how they can delete the entire lines when a certain cell is empty. First enter xxx where you want the loop to stop. Select the cell at the top of the column and  run this macro.

Sub proDelete()

        Do Until Selection.Value = "xxx"
                  If Selection.Value = "" Then
                          Selection.Offset(1, 0).Select
                  End If


End Sub

Exiting a Loop

In the loop above if you want the loop to stop when it finds the value 99 you can add this line of code within the loop:
If Selection.Value = 99  Then Exit Do

Exit allows you to get out of almost anything like:
Exit Sub
Exit For
Exit Do

Go to the next lesson
21 : Functions in VBA for Excel

left arrow Back home