0% found this document useful (0 votes)
106 views

VBA Lesson 20: VBA For Excel Statements

This document discusses various VBA statements that can be used in Excel macros including If, Do, For, Select Case statements. It also provides a code sample of a VBA macro that loops through a range of cells and deletes entire rows where the cell is empty. The macro uses a Do loop and If/Then statement to check each cell for empty values and deletes the row if empty, otherwise moves to the next row. It also mentions how to exit a loop early using the Exit statement.

Uploaded by

prasadramanji07
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views

VBA Lesson 20: VBA For Excel Statements

This document discusses various VBA statements that can be used in Excel macros including If, Do, For, Select Case statements. It also provides a code sample of a VBA macro that loops through a range of cells and deletes entire rows where the cell is empty. The macro uses a Do loop and If/Then statement to check each cell for empty values and deletes the row if empty, otherwise moves to the next row. It also mentions how to exit a loop early using the Exit statement.

Uploaded by

prasadramanji07
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

VBA Lesson 20: VBA for Excel Statements Among the VBA statements that you will discover

in the downloadable tutorial on Excel macros, there are the "If" statement including Then, ElseIf and End If, there is the "Do" statement including Loop, Until, While and Exit, there is the "For" statement including To, Step, Next and Exit, there is the powerful "Select Case" statement including Case, End Select and Exit and other statements. A lot of visitors ask us how they can delete the entire lines when a certain cell is empty. For example, in the table below rows 2 and 5 should be deleted:

First enter xxx where you want the loop to stop (below the last value: B7). Select the cell at the top of the column containing the values to be considered (B1)and run the macro. Sub proDelete() Range("B1").Select Do Until Selection.Value = "xxx" If Selection.Value = "" Then Selection.EntireRow.Delete Else Selection.Offset(1, 0).Select End If Loop Range("A1").Select End Sub If you have completed the free exercises "Free Basics", just copy/paste the macro above in the Visual Basic editor and run it. 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

You might also like