Excel VBA to Loop through Range until Empty Cell (4 Examples)

In our sample dataset, some cells in the Salesman column are empty. We’ll use VBA loops to locate these empty cells.

vba loop through range until empty


Example 1 – Excel VBA to Loop through Known Number of Rows until Single Empty Cell

This method is suitable for smaller datasets where you know the number of rows. It finds the first empty cell in a specified column.

STEPS:

  • Right-click the sheet tab and select View Code.

Excel VBA to Loop through Known Number of Range until Single Empty Cell

  • The VBA window dialog box will pop up.
  • Paste the following code.
Sub SingleEmpty()
      Dim i As Integer
      Application.ScreenUpdating = False
      NumRows = Range("B5", Range("B5").End(xlDown)).Rows.Count
      Range("B5").Select
      For i = 1 To NumRows
         ActiveCell.Offset(1, 0).Select
      Next
      Application.ScreenUpdating = True
End Sub

Excel VBA to Loop through Known Number of Range until Single Empty Cell

  • Close the VBA window.
  • Go to the Developer tab, select Macros.

Excel VBA to Loop through Known Number of Range until Single Empty Cell

  • In the Macro dialog box, select SingleEmpty and press Run.

  • The first empty cell (e.g., B7) will be highlighted.


Example 2 – VBA to Loop through Known Number of Rows until Continuous Blank Cells

This method finds the first empty cell followed by one or more empty cells.

VBA to Loop through Known Number of Rows until Continuous Blank Cells

STEPS:

  • Right-click the sheet tab and select View Code.
  • Paste the following code in the VBA window.
Sub ContinuousBlankCells()
Dim rg As Range
On Error Resume Next
      Set rg = Application.InputBox _
        (Prompt:="Select First Cell", Title:="LoopThroughUntilEmpty", Type:=8)
      rg.Cells(1, 1).Select    
      Application.ScreenUpdating = False
      Do Until IsEmpty(ActiveCell) And IsEmpty(ActiveCell.Offset(1, 0))
         ActiveCell.Offset(2, 0).Select
      Loop
      Application.ScreenUpdating = False
End Sub

VBA to Loop through Known Number of Rows until Continuous Blank Cells

  • Close the VBA window.
  • Go to Developer Macros.
  • In the Macro dialog box, select ContinuousBlankCells and click Run.

VBA to Loop through Known Number of Rows until Continuous Blank Cells

  • In the dialog box, select cell B5 in the Select First Cell box.
  • Click OK.

  • The code will highlight the desired cell (e.g., B9).

Read More: VBA to Loop through Rows of Table in Excel


Example 3 – Loop through Unknown Number of Rows with VBA until Empty Cell

Loop through Unknown Number of Rows with VBA until Empty Cell

STEPS:

  • Right-click the sheet tab and select View Code.
  • Paste the following code in the VBA window.
Sub UnknownNumRows()
      Range("D5").Select
      Do Until IsEmpty(ActiveCell)
         ActiveCell.Offset(1, 0).Select
      Loop
End Sub

Loop through Unknown Number of Rows with VBA until Empty Cell

  • Close the VBA window.
  • Go to Developer Macros.
  • In the Macro dialog box, select UnknownNumRows and click Run.

  • The empty cell (e.g., D8) will be highlighted.


Example 4 – Excel VBA to Loop through Specified Range until Blank Cell

This method loops through a specific range (e.g., B5:D10) to find the first empty cell.

Excel VBA to Loop through Specified Range until Blank Cell

STEPS:

  • Right-click the sheet tab and select View Code.
  • Paste the following code in the VBA window.
Sub LoopThruRange()
      Range("B5:D10").Select
      Do Until IsEmpty(ActiveCell)
         ActiveCell.Offset(1, 0).Select
      Loop
End Sub

Excel VBA to Loop through Specified Range until Blank Cell

  • Close the VBA window.
  • Go to Developer Macros.
  • In the Macro dialog box, select LoopThruRange and click Run.

  • The empty cell (e.g., B9) will be highlighted.


Download Practice Workbook


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Aung Shine
Aung Shine

Aung Shine completed his bachelor’s in Electrical and Electronics Engineering from Bangladesh University of Engineering and Technology. It has been almost 2 years since he joined SOFTEKO and actively working on the ExcelDemy project. Currently he works as a Team Leader where he guides his team members to create technical content. He has published 150+ articles and reviewed 50+ articles. He has also solved various user problems before. He has interests in Data Analysis, Power Query, Advanced Excel,... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo