Select the product you need help with
- Internet Explorer
- Windows Phone
- More products
How to loop through a list of data on a worksheet by using macros in Excel
Article ID: 299036 - View products that this article applies to.
This article was previously published under Q299036
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:
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 RowsThis code moves down column A to the end of the list:
To Search a Dynamic List or a List with an Unknown Number of RowsThis 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.)
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:
To Search a List for a Specific RecordThis code moves down column A to the end of the list:
Article ID: 299036 - Last Review: September 18, 2011 - Revision: 8.0