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
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()
'UpdatebyExtendoffice20161222
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.
Range("A1").Select
' 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
Next
Application.ScreenUpdating = True
End Sub
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.
Note: If you want to loop through data until meet continuous blank cells, you can use this macro code.
Sub LoopThroughUntilBlanks()
'UpdatebyExtendoffice20161222
' 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
Loop
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.
Unlock Excel Magic with Kutools AI
- Smart Execution: Perform cell operations, analyze data, and create charts—all driven by simple commands.
- Custom Formulas: Generate tailored formulas to streamline your workflows.
- VBA Coding: Write and implement VBA code effortlessly.
- Formula Interpretation: Understand complex formulas with ease.
- Text Translation: Break language barriers within your spreadsheets.
Best Office Productivity Tools
Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time. Click Here to Get The Feature You Need The Most...
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!