Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in


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

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 60 days free trial!
combine sheets
Kutools for Excel: with more than 300 handy Excel add-ins, free to try with no limitation in 60 days.

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

Tabbed browsing & editing multiple Excel workbooks/Word documents as Firefox, Chrome, Internet Explore 10!

You may be familiar to view multiple webpages in Firefox/Chrome/IE, and switch between them by clicking corresponding tabs easily. Here, Office Tab supports similar processing, which allow you to browse multiple Excel workbooks or Word documents in one Excel window or Word window, and easily switch between them by clicking their tabs. Click for free 45-day trial of Office Tab!

ot excel

Recommended Productivity Tools

Ribbon of Excel (with Kutools for Excel installed)

300+ Advanced Features Increase Your Productivity by 71%, and Help You To Stand Out From Crowd!

Would you like to complete your daily work quickly and perfectly? Kutools For Excel brings 300+ cool and powerful advanced features (Combine workbooks, sum by color, split cell contents, convert date, and so on...) for 1500+ work scenarios, helps you solve 82% Excel problems.

  •  Deal with all complicated tasks in seconds, help to enhance your work ability, get success from the fierce competition, and never worry about being fired.
  •  Save a lot of work time, leave much time for you to love and care the family and enjoy a comfortable life now.
  •  Reduce thousands of keyboard and mouse clicks every day, relieve your tired eyes and hands, and give you a healthy body.
  •  Become an Excel expert in 3 minutes, and get admiring glance from your colleagues or friends.
  •  No longer need to remember any painful formulas and VBA codes, have a relaxing and pleasant mind, give you a thrill you've never had before.
  •  Spend only $39, but worth than $4000 training of others. Being used by 110,000 elites and 300+ well-known companies.
  •  60-day unlimited free trial. 60-day money back guarantee. Free upgrade and support for 2 years. Buy once, use forever.
  •  Change the way you work now, and give you a better life immediately!

Office Tab Brings Efficient And Handy Tabs to Office (include Excel), Just Like Chrome, Firefox, And New IE

  • Increases your productivity by 50% when viewing and editing multiple documents.
  • Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
  • Open and create documents in new tabs of same window, rather than in new windows.
  • Help you work faster and easily stand out from the crowd! One second to switch between dozens of open documents!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Robert · 3 months ago
    The loop works for me except it loops through every single row regardless of it being blank or not.
  • To post as a guest, your comment is unpublished.
    Shane · 8 months ago
    Lets say I have a ton of rows...any tricks on making this run faster?
  • To post as a guest, your comment is unpublished.
    Marija · 10 months ago
    Can you please explain what is the condition to stop looping? What makes you break out of the loop in Test1()?
    • To post as a guest, your comment is unpublished.
      Sunny · 9 months ago
      It will stop while meeting the first blank in the column
  • To post as a guest, your comment is unpublished.
    J · 1 years ago
    Its looping a column not a row
    • To post as a guest, your comment is unpublished.
      SomeGuest · 10 months ago
      In order to loop through a column, you must go by row number.