Loop through a Range of Cells in Excel VBA/Macros

Add to Favorites
Author:

How to use VBA/Macros to iterate through each cell in a range, either a row, a column, or a combination of both. This is for when you need to look into each cell or do something with each cell in a range; if you just need to search for a piece of data within a range, use the Find method (link coming soon).

Sections:

How to Loop Through a Range of Cells

Loop through a User-Selected Range

Selecting Multiple Contiguous Rows and Columns

Proper Technique (Doesn't Matter)

Notes

How to Loop Through a Range of Cells

Here I will hard-code a specific range in Excel and create the code needed to loop or iterate through that range.

Code:

This is the full code.  Below, I will explain it.

'get the range through which we want to loop
Set cell_range = Range("A1:A10")

'start the loop
For Each cell In cell_range

    'you are now inside the loop
    MsgBox "Cell value = " & cell.Value

'continue the loop
Next cell

Get the Range to Loop Through

f64543872feb401157cee4791df71d07.jpg

Set cell_range = Range("A1:A10")

Here, the range A1:A10 has been assigned to the variable cell_range.  You can name the variable whatever you want basically but not "Range".

I have hard-coded the range here but, below, I will show you another way to get the range, including by user selection.

Loop through the Range

Now that we have a range, we can loop through it.

There are a few ways to do this and I will show you the easiest method here.

We use the For EachNext loop.  It looks like this:

'start the loop
For Each cell In cell_range

    'you are now inside the loop

'continue the loop
Next cell

Put this under the code we already have like this:

abe1ec9c66388a7f27ea9b4d03de10a3.jpg

This is the most basic way to loop through a range of cells in Excel using VBA/Macros.

The loop always has this format:

For Each current_cell_variable_name In range_reference(can also be a variable like in this example)

                What you want to do in the loop here.

Next current_cell_variable_name

The only thing that you need to understand about this loop is where to put two variables.  You need a variable that will represent the current cell that you are looping through, I called this cell in my example, and you need a variable that contains a range reference, I called this cell_range in my example.

cell_range is just what contains the range reference.  You can use an actual range reference here if you want instead of using a variable that contains a range reference, but putting it into a variable like in this example will make it easier to maintain in the future.

cell is just a variable that you use ONLY for this loop.  You can name it what you want, but make sure to put it after where it says "For Each" AND after where you put "Next".

Test the Loop

I will output a simple message box that shows the value of each cell so you can see that it works.

Here is the complete code (the macro has been named loop_range_1 now):

'get the range through which we want to loop
Set cell_range = Range("A1:A10")

'start the loop
For Each cell In cell_range

    'you are now inside the loop
    MsgBox "Cell value = " & cell.Value

'continue the loop
Next cell

8b65106ee04160c6371a5314e29121aa.jpg

Go back to Excel, hit Alt + F8 and select the macro and hit Run and we see this:

b2bdce2f91cede872ee6087e692a3845.jpg

The box will iterate through all 10 cells in the range and display the value from each cell.

Loop through a User-Selected Range

We follow the example from above but change one little thing:

'get the range through which we want to loop
Set cell_range = Selection

'start the loop
For Each cell In cell_range

    'you are now inside the loop
    MsgBox "Cell value = " & cell.Value

'continue the loop
Next cell
 

I replaced Range("A1:A10") with Selection. Nothing else was changed.

Now, go back to Excel, select a range of cells, hit Alt + F8 and click the macro loop_range_selection and hit run and you will see it work.

4f8739d3414e1ae0812fc7837b5c008d.jpg

Selecting Multiple Contiguous Rows and Columns

This works exactly the same as the previous examples. Excel doesn't care if you select data from one row, one column, or multiples of each. It will iterate through the cells from left-to-right until it hits the end of the range and then it will go down to the next row and do the same thing.

Proper Technique (Doesn't Matter)

Technically, you are supposed to declare all variables at the top of the macro but, for such simple things as Excel macros, this is almost never important. As such, I left that out here. If you want, you can read about variables in our other tutorials and add those, but, trust me, it is probably not worth your time and, if it is, then you are creating a macro so sophisticated that you already know about variables and won't be reading this tutorial in the first place.

Notes

This is a pretty basic technique in Excel. It has only two parts, figuring out the range through which to loop and looping through that range.

In this example, if you select empty cells and iterate through them, there will be nothing after the "Cell value = " text in the pop-up message box.

(If you don't know how to install/create a macro, read this tutorial: Install a Macro in Excel)

Download the accompanying Excel file to use the macros from this tutorial.


Downloadable Files: Excel File

Question? Ask it in our Excel Forum


Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons 50+ Hours of Instruction 200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Similar Content on TeachExcel
Loop through a Range of Cells in a UDF in Excel
Tutorial: How to loop through a range of cells in a UDF, User Defined Function, in Excel. This is ...
Count The Number of Words in a Cell or Range of Cells in Excel - With User-Specified Delimiter / Separator - UDF
Macro: UDF to count the number of words in a cell or range with a user-specified delimiter. ...
Select Ranges of Cells in Excel using Macros and VBA
Tutorial: This Excel VBA tutorial focuses specifically on selecting ranges of cells in Excel.  This...
Count The Number of Words in a Cell or Range of Cells in Excel - UDF
Macro: Count words in cells with this user defined function (UDF). This UDF allows you to count t...
Select Cells in Excel using Macros and VBA
Tutorial: This is actually a very easy thing to do and only requires a couple lines of code. Below I...
Pop-Up Message Box When a Range of Cells Reaches a Certain Average
Macro: This macro will display a message box when the numbers within a range of cells reaches an ...
Tutorial Details
Downloadable Files: Excel File
Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course