How to Use VBA for Each Cell in a Range in Excel (3 Methods)

When working with large databases, dealing with ranges and cells is a common task. Sometimes, you need to perform the same action across a large range or a significant number of cells. Manually repeating this process can be time-consuming and inefficient. However, you can create a VBA (Visual Basic for Applications) code that runs through each cell in a specified range and performs the desired action. In this article, we’ll explore three methods for achieving this in Excel.

vba for each cell in range in excel


Method 1 – Applying VBA for Each Cell in a Range

Let’s consider a scenario where you want to apply the same VBA code to each cell in a given range (e.g., B4:F13). Follow these steps:

Basic outline for applying vba code for each cell in range

  • Go to the Developer tab.
  • Select Insert and choose the command button to add it to your worksheet.

Inserting a command button from control panel

The command button has been added.

Having a command button from control panel

  • Right-click on the command button and select Properties.

Click on properties option to label your command prompt..

  • Change the caption (name) of the button (e.g., Click Here).

Choose the command prompt caption

  • Double-click the command button to open the VBA Module.
  • Declare two variables for the range:
Dim CL As Range
Dim Rng As Range

Apply a VBA Code for Each Cell in a Range

  • Assign the specific range you want to work with:
Set Rng = Worksheets("Each Cell in Range").Range("B4:F13")

Writing vba code to specify the worksheet.

  • Use a loop to perform an action on each cell in the range:
For Each CL In Rng
CL.Value = 100
Next CL

Apply a VBA Code for Each Cell in a Range

The final code is:

Private Sub CommandButton1_Click()
Dim CL As Range
Dim Rng As Range
Set Rng = Worksheets("Each Cell in Range").Range("B4:F13")
For Each CL In Rng
CL.Value = 100
Next CL
End Sub
  • Return to your main worksheet.
  • Click the Click Here command button to execute the VBA code.

Run the vba code

  • You can also apply text values to each cell in the range. Instead of CL.Value = 100, use your desired text value (e.g., “ExcelDemy”).
CL.Value = “ExcelDemy”

Apply a VBA Code for Each Cell in a Range

  • Click on the command button and the VBA code will return this text value for each cell in the range.

Output of a VBA Code for Each Cell in a Range

  • To highlight blank cells within the range, add the following condition to your existing code:

Apply a VBA Code for Each Cell in a Range

If CL.Value = "" Then
CL.Interior.ColorIndex = 7
End If
  • In addition, the new code will highlight the blank cell with red color. So, the full code is,
Private Sub CommandButton1_Click()
Dim CL As Range
Dim Rng As Range
Set Rng = Worksheets("Each Cell in Range").Range("B4:F13")
For Each CL In Rng
If CL.Value = "" Then
CL.Interior.ColorIndex = 7
End If
Next CL
End Sub

Apply a VBA Code for Each Cell in a Range

  • Click the command button to see the results.

Apply a VBA Code for Each Cell in a Range


Method 2 – Inserting VBA Code for Each Cell in a Column

You can also run VBA code for each cell in a column. Let’s say you have a column containing numbers, and you want to color values that are lower than 10. Follow these steps:

Dataset for inserting vba code for column cells

  • Follow the instructions we discussed earlier to insert a command button.

Click on the command prompt

  • Double-click the command button to open the VBA window.
  • Enter the following code in your VBA window:
Private Sub CommandButton1_Click()
Dim c As Long
Columns(2).Font.Color = vbBlack
For c = 1 To Rows.Count
If Cells(c, 2).Value < Range("D5").Value _
And Not IsEmpty(Cells(c, 2).Value) Then
Cells(c, 2).Font.Color = vbRed
End If
Next c
End Sub

Code Breakdown

The code is divided into 2 steps.

  • In the first part, we set c as a Long variable, and the font color is black.
  • The For loop checks each row to see if the value exceeds that of cell D5. If not, the font color is set to Red.
  • Click the command button to execute the VBA code.

Output of vba code on column cells

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


Method 3 – Employing VBA for Each Cell in a Row

You can also run a VBA code for each cell in a row. Follow these steps:

Dataset for writing a VBA Code For Each Cell in a Row

  • Add a command button and change its name to Click Here!

Add a click here command button

  • Double-click on the command button to open the VBA window and enter the code below:
Private Sub CommandButton1_Click ()
Dim r As Range
Dim MyString As String
'For Each cell in a row, and apply a yellow color fill
For Each r In Range("B3:F3").Rows
r.Interior.ColorIndex = 6
Next
End Sub

Code Breakdown

The code is divided into 2 steps.

  • In the first part, r is taken as Range and Mystring as String variable.
  • In the second part, the For loop fills the cells in a specific row with yellow color (Interior.ColorIndex = 6).

VBA code for each cell on a specific row

  • Click the command button to see the results.

Output of vba code for each cell in a row


How to Use Excel VBA to Loop Through a Range Until a Single Empty Cell

View the dataset given below, where you will see B7 cell is empty. Let’s develop a VBA code to find the empty cell.

Dataset for VBA Loop Through Range until Single Empty Cell

If you’re dealing with a large dataset and need to find an empty cell, enter the following VBA code:

  • Press Alt + F11 to open Microsoft Visual Basic for Applications.
  • Enter the following VBA code in Sheet4:
Sub FirstEmpty()
      Dim A As Integer
      Application.ScreenUpdating = False
      ROWNUM = Range("B5", Range("B5").End(xlDown)).Rows.Count
      Range("B5").Select
      For A = 1 To ROWNUM
         ActiveCell.Offset(1, 0).Select
      Next
      Application.ScreenUpdating = True
End Sub
  • This code finds the first empty cell in column B starting from B5.

VBA code for Range until Single Empty Cell

The output is given below:

Read More: Loop through a Range for Each Cell with Excel VBA


Things to Remember

If your developer tab isn’t visible, activate it via: Customized Quick Access Toolbar → More Commands → Customize Ribbon → Developer → OK


Download Practice Workbook

You can download the practice workbook from here:


Similar Articles to Explore 

Get FREE Advanced Excel Exercises with Solutions!

Tags:

Asikul Himel
Asikul Himel

Asikul Islam Himel, holding a BSc in Naval Architecture and Marine Engineering from Bangladesh University of Engineering and Technology, has contributed over two years to the ExcelDemy project. Starting as an Excel & VBA Content Developer, now he manages projects at You Have Got This Math Project. He wrote 60+ articles for ExcelDemy, reviewed 500+, and focused on quality maintenance. Currently, his responsibilities include project management and team leadership. Himel's interests encompass data analysis, leadership, WordPress applications, and... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo