How to Loop through a Range for Each Cell Using Excel VBA (8 Examples)

We will show you step-by-step instructions on applying a loop for each cell in a range using Excel VBA with a simple example. Look at this dataset below, which has some values in a column. We aim to loop through all of them and add five with each value.

dataset for each cell in range vba excel

 

Step 1: Open the VBA Editor in Excel

  • Press Alt+F11. Select Insert > Module.

insert VBA module in excel

Step 2: Build the Code

  • Enter the following code in the editor:
Sub add_five()
Dim cell As Range
For Each cell In Range("B5:B9")
 cell.Value = cell.Value + 5
Next cell
End Sub

Step 3: Run the Code

  • Press Alt+F8.
  • Select add_five.
  • Click Run. You will see this output:

output of excel for each cell in range vba

We have successfully added five with each of those numbers.

Breakdown of the Code

For each cell in Range(“B5:B9”): Here, our working range was B5 to B9.

working range of the datatset

From that loop, our cell variable will point to cell B5.

first pointed cell in Excel

It will add 5 with the cell value and update it.

add 5 with the first value

The cell variable will point to cell B6.

newly pointed cell

Similar to the previous steps, it will add five with the cell value and update it.

updated value in the dataset

All the cells will go through this process, and the code will add five for each.

output of excel for each cell in range vba

We successfully updated all the values using the “for each cell in range” loop.


Method 1 – Looping through an Entire Row

values to work with

  • If you don’t want to select a particular range but the entire row, enter the following code:
Sub entire_row()
Dim cell As Range
For Each cell In Range("2:2")
 cell.Value = cell.Value * 5
Next cell
End Sub

It will multiply each cell of row 2 with the value 5.

It will look like the following screenshot:

Loop through Entire Row

Read More: How to Use VBA for Each Row in a Range in Excel


Method 2 – Looping through an Entire Column

values to work with

  • If you want to loop through an entire column and perform something, enter the following code:
Sub entire_column()
Dim cell As Range
For Each cell In Range("B:B")
 cell.Value = cell.Value * 5
Next cell
End Sub

It will multiply each cell of column B with the value 5. After that, it will look like the following screenshot:

Loop through Entire Column


Method 3 – Looping through a User-selected Range in Excel

Steps:

  • If you don’t want to specify the range of cells but choose with your mouse, enter the following code:
Sub user_selected()
Dim cell As Range
Set range_of_cells = Selection
For Each cell In range_of_cells
 cell.Value = "Hello"
Next cell
End Sub

This code sets the cell’s value to “Hello”. Notice here that we didn’t specify any range of cells. Instead, we used Selection. It will perform this operation on a user-selected range. You can understand it by the following screenshot.

  • Select a range of cells:

Selecting the desired cells

  • Apply the macro. You will see the output.

Loop through User-selected Range in Excel


Method 4 – Looping through an Array For Each Range

  • If you have an array consisting of multiple items in it, enter this code to apply any kind of task:
Sub loop_array()
Dim array_value As Variant
Dim val As Variant
array_value = Array("Apple", "Orange", "Banana")

For Each val In array_value
 MsgBox val
Next val

End Sub

Here, we have some values in an array. We take them in for each loop and show them in a message box in Excel.

Loop through Array Using for Each Range


Method 5 – Sheets as a Range in VBA

You can also select sheets as a range in VBA. We can perform operations across multiple sheets.

Sub loop_sheets()
Dim sheet As Worksheet
For Each sheet In Sheets
sheet.Visible = True
Next sheet
End Sub

This code snippet will unhide all the sheets in a workbook.


Method 6 – Using Workbooks for Each Loop

The following code snippet will take workbooks as a range and close all the workbook at once.

Sub loop_workbooks()
Dim wBook As Workbook
For Each wBook In Workbooks
wBook.Close
Next wBook
End Sub

Read More: How to Use For Each Loop in Excel VBA


Method 7 – Looping through a Table in Excel

  • Select tables as a range in our VBA codes.
Sub loop_tables()
Dim table As ListObject
For Each table In Sheets("Table").ListObjects
 table.Delete
Next table
End Sub
  • This VBA code will take the table as a range from the sheet “Table” and delete it.

Loop through tables


Method 8 – Using Conditions for Each Cell in Range with VBA

If you have an idea about the If condition in Excel, you mix it up with the for each loop. This is one of the important tasks in VBA. The following sample will give a clear idea about that:

Sub loop_if()

Dim cell As Range

For Each cell In Range("C5:C9")
 If cell.Value < 33 Then
   cell.Offset(o, 1).Value = "Failed"
 Else
   cell.Offset(0, 1).Value = "Passed"
 End If
Next cell

End Sub
  • Apply this code to the following dataset:

dataset for each cell in range vba in Excel

Here, we have a dataset of some students’ marks. Our goal is to analyze their marks and give a remark on whether they passed or failed. If someone scores more than 32, he/she gets passing marks. Otherwise, it will consider the marks as failed.

If we run the code, we will get the following result:

output of Excel for each cell in range vba

Read More: How to Use VBA for Each Cell in Range in Excel


Things to Remember

Performing any operations throughout the whole columns or rows will change the whole columns/rows output.

Your loop variable should be the same type as the Range type you selected. If your range type is Worksheets, your loop variable should be Worksheet type.


Download the Practice Workbook

Download this workbook to practice.


Related Articles

Get FREE Advanced Excel Exercises with Solutions!

Tags:

A.N.M. Mohaimen Shanto
A.N.M. Mohaimen Shanto

A.N.M. Mohaimen Shanto, a B.Sc. in Computer Science and Engineering from Daffodil International University, boasts two years of experience as a Project Manager at Exceldemy. He authored 90+ articles and led teams as a Team Leader, meticulously reviewing over a thousand articles. Currently, he focuses on enhancing article quality. His passion lies in Excel VBA, Data Science, and SEO, where he enjoys simplifying complex ideas to facilitate learning and growth. His journey mirrors Exceldemy's dedication to excellence and... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo