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).
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)
Here I will hard-code a specific range in Excel and create the code needed to loop or iterate through that range.
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
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.
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:
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".
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
Go back to Excel, hit Alt + F8 and select the macro and hit Run and we see this:
The box will iterate through all 10 cells in the range and display the value from each cell.
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.
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.
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.
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.