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.
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. strong>Click for full-featured 30 days free trial!|
|Kutools for Excel: with more than 300 handy Excel add-ins, free to try with no limitation in 30 days.|
- Reuse Anything: Add the most used or complex formulas, charts and anything else to your favorites, and quickly reuse them in the future.
- More than 20 text features: Extract Number from Text String; Extract or Remove Part of Texts; Convert Numbers and Currencies to English Words.
- Merge Tools: Multiple Workbooks and Sheets into One; Merge Multiple Cells/Rows/Columns Without Losing Data; Merge Duplicate Rows and Sum.
- Split Tools: Split Data into Multiple Sheets Based on Value; One Workbook to Multiple Excel, PDF or CSV Files; One Column to Multiple Columns.
- Paste Skipping Hidden/Filtered Rows; Count And Sum by Background Color; Send Personalized Emails to Multiple Recipients in Bulk.
- Super Filter: Create advanced filter schemes and apply to any sheets; Sort by week, day, frequency and more; Filter by bold, formulas, comment...
- More than 300 powerful features; Works with Office 2007-2019 and 365; Supports all languages; Easy deploying in your enterprise or organization.
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.
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 30-day trial of Office Tab!
You are guest
or post as a guest, but your post won't be published automatically.
- To post as a guest, your comment is unpublished.· 1 years agoThe 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.· 1 years agoLets say I have a ton of rows...any tricks on making this run faster?
- To post as a guest, your comment is unpublished.· 1 years agoCan 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.· 1 years agoIt will stop while meeting the first blank in the column
- To post as a guest, your comment is unpublished.· 2 years agoIts looping a column not a row
- To post as a guest, your comment is unpublished.· 1 years agoIn order to loop through a column, you must go by row number.