Run VBA Do While Loop Till Cell Is Not Empty in Excel: 3 Suitable Methods

Method 1 – Using VBA Do While Loop to Print Values Till Cell Is Not Empty

  • We prepared a dataset with Employee ID, Name, Designation We have included data for the columns and left a space in the Name column so that we can check it out with VBA code.

dataset for using do while loop to print values till cell is not empty

  • Open the VBA window and Insert a new module.
  • Write the following code and Run

Code:

Sub Print_Untill_Empty_Cell()
Dim currentCell As Range
Set currentCell = Range("C5")
Do While currentCell.Value <> ""
' Do something with the non-empty cell
Debug.Print currentCell.Value
' Move to the next cell in the column
Set currentCell = currentCell.Offset(1, 0)
Loop
End Sub

code to use do while to print until cell is empty

Code Breakdown

  • We start by defining a new sub-procedure called Print_Until_Empty_Cell.
  • We declare a variable called currentCell as a Range object and set it to the cell C5 using the Range function.
  • We use a Do While loop to loop through the cells in column C until an empty cell is encountered. We check if the current cell is not empty using the condition Value <> “”.
  • Inside the loop, we use the Print statement to print the value of the current cell to the Immediate window.
  • We use the Offset method to move to the next cell in the column by setting currentCell to the cell one row below the current cell using the Offset(1, 0) method.
  • The loop continues until an empty cell is encountered in column C.

  • We will see data from our worksheet column Name displayed in the immediate window until an empty cell is found in the column. Look carefully. Lenon’s name is not displayed because there’s a space before it in column C.

Printed values from the column until the code finds an empty cell


Method 2 – Checking Numeric Values with Do While Loop Till Cell Is Not Empty

  • We have some numeric numbers in the Employee ID And we kept a cell empty (B11) for testing the empty cell.

dataset with an empty cell in the employee id column

  • Copy the following code into a new module and run it.

Code:

Sub Check_Numeric()
Dim currentCell As Range
Set currentCell = Range("B5")
Do While currentCell.Value <> ""
' Do something with the non-empty cell
If IsNumeric(currentCell.Value) Then
Debug.Print "The value in cell " & currentCell.Address & " is " & currentCell.Value
End If
' Move to the next cell in the row
Set currentCell = currentCell.Offset(1, 0)
Loop
End Sub

code to check the numeric values until empty cell appears

Code Breakdown

  • We use a Do While loop to loop through the cells in row 5 until an empty cell is encountered.
  • We check if the current cell is not empty using the condition Value <> “”.
  • Inside the loop, we use the IsNumeric function to check if the value of the current cell is numeric. If it is, we use the Print statement to print a message to the Immediate window that tells us the value of the cell.
  • We use the Offset method to move to the next cell in the row by setting currentCell to the cell one column to the right of the current cell using the Offset(0, 1) method.
  • The loop continues until an empty cell is encountered in row 5.
  • After running the code, we will see the numeric values displayed until the code finds an empty cell in the worksheet column in the immediate window of VBA.

Result of checking numeric till empty cell is found


Method 3 – Copy Cell Values to Another Sheet with Do While Loop Until Empty Cell Is Found

  • Observe we have an empty space at the end of the data of column Name.

dataset to copy values to another sheet until an empty cell is found

  • Write the following code in a new module and run it.

Code:

Sub Copy_until_NonEmptyCells()
Dim currentCell As Range
Dim outputSheet As Worksheet
Dim outputRow As Long
' Set the output worksheet and starting row
Set outputSheet = Worksheets("Output")
outputRow = 5
' Set the starting cell in the column
Set currentCell = Range("C5")
' Loop through the column until an empty cell is encountered
Do While currentCell.Value <> ""
' Write the value to the output worksheet
outputSheet.Cells(outputRow, 2).Value = currentCell.Value
' Move to the next cell in the column
Set currentCell = currentCell.Offset(1, 0)
outputRow = outputRow + 1
Loop
End Sub

code to copy values to other sheet till empty cell is not found

Code Breakdown

  • We set the outputSheet variable to the worksheet named “Output” using the Worksheets function and set the outputRow variable to 5.
  • We set the currentCell variable to the cell C5 using the Range
  • We use a Do While loop to loop through the cells in column C until an empty cell is encountered. We check if the current cell is not empty using the condition Value <> “”.
  • Inside the loop, we use the Cells property of the outputSheet object to write the value of the current cell to the corresponding cell in column B of the outputSheet.
  • We use the Offset method to move to the next cell in the column by setting currentCell to the cell one row below the current cell using the Offset(1, 0) method.
  • We also increment the outputRow variable by 1 to move to the next row in column B of the outputSheet.
  • The loop continues until an empty cell is encountered in column C.

  • We will see those data copied to another “Output” worksheet.

copied data from another sheet till the empty cell is found


How to Run VBA For Loop Till Cell Is Not Empty in Excel.

  • Observe we have empty cells in column E.

print data until an empty cell is found using for loop

  • Copy the following code in a new module and run it.

Code:

Sub For_Loop_NonEmpty_Cells()
Dim currentCell As Range
Dim lastColumn As Long
Dim rowToCheck As Long
' Set the row to check and the last column in the row
rowToCheck = 5
lastColumn = Cells(rowToCheck, Columns.Count).End(xlToLeft).Column
' Loop through the cells in the row
For Each currentCell In Range(Cells(rowToCheck, 1), Cells(rowToCheck, lastColumn))
' Check if the cell is non-empty
If currentCell.Value <> "" Then
' Do something with the non-empty cell
Debug.Print "The value in cell " & currentCell.Address & " is " & currentCell.Value
End If
Next currentCell
End Sub

code to print data until empty cell is found using for loop

Code Breakdown

  • We set rowToCheck to 5, which is the row we want to check.
  • We use the Cells function to determine the last column in the row by starting at cell A5 and using the End(xlToLeft) method to find the last cell with data in the row. We then assign the column number to the lastColumn variable.
  • We use a For Each loop to loop through the cells in the row, starting at cell A5 and ending at the last non-empty cell in the row.
  • Inside the loop, we use an If statement to check if the current cell is non-empty using the condition Value <> “”.
  • If the cell is non-empty, we use the Print statement to print the value of the current cell and its address to the Immediate window.
  • The loop continues until all non-empty cells in the row have been checked.
  • We will see the cell address and values displayed at the immediate window till an empty cell is found in the worksheet row.

printed data using for loop till empty call is not found


Frequently Asked Questions

  • What is a Do While loop in VBA?

Ans: A Do While loop is a control structure in VBA that allows you to execute a block of code repeatedly while a specific condition is true. The loop continues to run as long as the condition specified after the Do While keyword is true. Once the condition becomes false, the loop terminates and the code moves to the next commands.

  • Can I use a For loop instead of a Do While loop to iterate through cells in Excel?

Ans: Yes, you can use a For loop instead of a Do While loop to iterate through cells in Excel. To do this, you would define a range of cells to loop through and then use a For Each loop to iterate through each cell in the range.

  • Can I use a Do Until loop instead of a Do While loop to iterate through cells in Excel?

Ans: Yes, you can use a Do Until loop instead of a Do While loop to iterate through cells in Excel. The Do Until loop is similar to the Do While loop, except that it continues to execute as long as the condition specified after the Do Until keyword is false.


Things to Remember

  • Don’t forget to clear the immediate window of VBA before running the next code.
  • Remember to save the file as the xlsm file before running codes.
  • Be careful about the cell references in the codes.

Download Practice Workbook

You can download the practice workbook from here.


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Mehedi Hasan Shimul
Mehedi Hasan Shimul

Md. Mehedi Hasan, with a BSc in Electrical & Electronic Engineering from Bangladesh University of Engineering and Technology, holds a crucial position as an Excel & VBA Content Developer at ExcelDemy. Driven by a deep passion for research and innovation, he actively immerses himself in Excel. In his role, Mehedi not only skillfully addresses complex challenges but also exhibits enthusiasm and expertise in gracefully navigating tough situations, emphasizing his steadfast commitment to consistently deliver exceptional and quality content.... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo