Note: The other languages of the website are Google-translated. Back to English

How to loop through rows until blank in Excel column?

Here is a long column with data which is including some empty cells, and you want to loop through the rows until meeting blank cell. In Excel, there is no built-in feature can handle this job, but I can introduce some macros to do you a favor.

Loop through rows until blank with VBA

arrow blue right bubble Loop through rows until blank with VBA

1. Press Alt + F11 keys to enable the Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste below code to the blank script.

VBA: Loop until blank

Sub Test1()
      Dim x As Integer
      Application.ScreenUpdating = False
      ' Set numrows = number of rows of data.
      NumRows = Range("A1", Range("A1").End(xlDown)).Rows.Count
      ' Select cell a1.
      ' Establish "For" loop to loop "numrows" number of times.
      For x = 1 To NumRows
         ' Insert your code here.
         ' Selects cell down 1 row from active cell.
         ActiveCell.Offset(1, 0).Select
      Application.ScreenUpdating = True
End Sub

doc loop until blank 1

In the code, A1 is the first cell you want to loop from, you can change it as you need.

3. Press F5 key to begin looping the column, then the cursor will stop at the first met blank cell.
doc loop until blank 2

Note: If you want to loop through data until meet continuous blank cells, you can use this macro code.

Sub LoopThroughUntilBlanks()
      ' Select cell A2, *first line of data*.
      Dim xrg As Range
      On Error Resume Next
      Set xrg = Application.InputBox _
        (Prompt:="first cell select..", Title:="Kutools for Excel", Type:=8)
      xrg.Cells(1, 1).Select
      ' Set Do loop to stop when two consecutive empty cells are reached.
      Application.ScreenUpdating = False
      Do Until IsEmpty(ActiveCell) And IsEmpty(ActiveCell.Offset(1, 0))
         ' Insert your code here.
         ' Step down 2 rows from present location.
         ActiveCell.Offset(2, 0).Select
      Application.ScreenUpdating = False
End Sub

Then you need to select the first cell you want to loop from in the Kutools for Excel dialog, click OK, then the cursor stops in the first continuous blank cells.

doc loop until blank 3 doc loop until blank 4

Easily Combine multiple sheets/Workbook into one Single sheet or Workbook

To combinne multiples sheets or workbooks into one sheet or workbook may be edious in Excel, but with the Combine function in Kutools for Excel, you can combine merge dozens of sheets/workbooks into one sheet or workbook, also, you can consolidate the sheets into one by several clicks only.  Click for full-featured 30 days free trial!
combine sheets
Kutools for Excel: with more than 300 handy Excel add-ins, free to try with no limitation in 30 days.

The Best Office Productivity Tools

Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

  • Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
  • Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
  • Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
  • Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
  • Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
  • Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
  • Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
  • Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
  • More than 300 powerful features. Supports Office/Excel 2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial. 60-day money back guarantee.
kte tab 201905

Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
officetab bottom
Comments (7)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Its looping a column not a row
This comment was minimized by the moderator on the site
In order to loop through a column, you must go by row number.
This comment was minimized by the moderator on the site
Can you please explain what is the condition to stop looping? What makes you break out of the loop in Test1()?
This comment was minimized by the moderator on the site
It will stop while meeting the first blank in the column
This comment was minimized by the moderator on the site
Lets say I have a ton of rows...any tricks on making this run faster?
This comment was minimized by the moderator on the site
The loop works for me except it loops through every single row regardless of it being blank or not.
This comment was minimized by the moderator on the site
The first VBA produces the wrong result in cases when there is one or zero rows of data.

You probably need something like

If Range("A1").Value = "" Then
NumRows = 0
ElseIf Range("A1").Offset(1, 0).Value = "" Then
NumRows = 1
NumRows = Range("A1", Range("A1").End(xlDown)).Rows.Count
End If
There are no comments posted here yet
Leave your comments
Posting as Guest
Rate this post:
0   Characters
Suggested Locations