How to loop through a list of data on a worksheet by using macros in Excel
Article
Applies to:
Microsoft Office Excel 2007, Microsoft Office Excel 2003, Excel 2010
Summary
When you write a Microsoft Visual Basic for Applications (VBA) macro, you may have to loop through a list of data on a worksheet. There are several methods for performing this task. The "More Information" section of this article contains information about the methods that you can use to search the following types of lists:
A list that contains a known, constant number of rows.
A dynamic list, or a list with an unknown number of rows.
A list that contains a specific record.
More Information
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. The following code samples assume that the list has a header row that starts in cell A1 and data that starts in cell A2.
To Search a List with a Constant, Known Number of Rows
This code moves down column A to the end of the list:
VB
Sub Test1()
Dim x AsInteger' Set numrows = number of rows of data.
NumRows = Range("A2", Range("A2").End(xldown)).Rows.Count
' Select cell a1.
Range("A2").Select' Establish "For" loop to loop "numrows" number of times.For x = 1To NumRows
' Insert your code here.' Selects cell down 1 row from active cell.
ActiveCell.Offset(1, 0).SelectNextEndSub
To Search a Dynamic List or a List with an Unknown Number of Rows
This code moves down column A to the end of the list. (This code assumes that each cell in column A contains an entry until the end.)
VB
Sub Test2()
' Select cell A2, *first line of data*.
Range("A2").Select' Set Do loop to stop when an empty cell is reached.DoUntil IsEmpty(ActiveCell)
' Insert your code here.' Step down 1 row from present location.
ActiveCell.Offset(1, 0).SelectLoopEndSub
Note If there are empty cells in column A throughout the data, modify this code to account for this condition. Make sure that the empty cells are a consistent distance apart. For example, if every other cell in column A is empty (for example, this situation may occur if every 'record' uses two rows, with the second row indented one cell), this loop can be modified as follows:
VB
' Set Do loop to stop when two consecutive empty cells are reached.DoUntil IsEmpty(ActiveCell) and IsEmpty(ActiveCell.Offset(1, 0))
' Insert your code here.'' Step down 2 rows from present location.
ActiveCell.Offset(2, 0).SelectLoop
To Search a List for a Specific Record
This code moves down column A to the end of the list:
VB
Sub Test3()
Dim x AsStringDim found AsBoolean' Select first line of data.
Range("A2").Select' Set search variable value.
x = "test"' Set Boolean variable "found" to false.
found = False' Set Do loop to stop at empty cell.DoUntil IsEmpty(ActiveCell)
' Check active cell for search value.If ActiveCell.Value = x Then
found = TRUEExitDoEndIf' Step down 1 row from present location.
ActiveCell.Offset(1, 0).SelectLoop' Check for found.If found = TrueThen
Msgbox "Value found in cell " & ActiveCell.Address
Else
Msgbox "Value not found"EndIfEndSub