How to Delete Row with VBA Macro If Cell Is Blank in Excel

In this article, I will show you how to delete row with VBA macro if the cell is blank in Excel. You’ll learn all the steps from opening the VBA window to extracting out the final output, with proper examples and illustrations. So, without any delay, let’s start the discussion.

Before starting the procedure, let’s have a quick look at the steps we are using to delete a row if the cell is blank.

Overview of Excel Macro Delete Row If Cell Is Blank


Download Practice Workbook


Step-by-Step Procedure to Delete Row with VBA Macro If Cell Is Blank in Excel

Here, we’ve got a data set with the Names of some Candidates, and their Marks in Preliminary, Written, and Viva of the recruitment test of a company called Jupyter Group.

Dataset for Applying Excel Macro to Delete Row if Cell Is Blank

In this dataset, we can see that some of the cells in this data set are blank. This means that candidate didn’t appear in that particular test. So, our objective is to delete the rows with blank cells from this data set.


Step 1: Open VBA Window

Firstly, we need to open the VBA window so that we can enter Macro. In the Macro mode, we can insert the VBA code.

➤ To do so, press ALT+F11 on your keyboard.

➤ As a result, it will open the VBA window.

Inserting VBA Window in Excel Workbook


Step 2: Enter New Module

In this step, we need to insert a module where we will type our code.

➤ In the beginning, go to Insert >> Module option in the VBA toolbar.

➤ Then, it will insert a new module called Module 1.

Insert New Module to Write Code


Step 3: Insert VBA Code in VBA Module

In this step, we will insert the VBA code in the module. As the outcome of the code, Excel will delete the row if the cell is blank.

➤ Now, insert the following VBA code in the module.

Sub Delete_Rows_with_Blank_Cells()
On Error GoTo Message
Column_Numbers = InputBox("Enter the Number of the Columns with the Blank Cells: ")
Column_Numbers = Split(Column_Numbers, " ")
Dim Count As Integer
Count = 0
For i = 1 To Selection.Rows.Count
    For j = 0 To UBound(Column_Numbers)
        If Selection.Cells(i, Int(Column_Numbers(j))) = "" Then
            Selection.Cells(i, 1).EntireRow.Delete
            i = i - 1
            Count = Count + 1
            Exit For
        End If
    Next j
    If Count >= Selection.Rows.Count Then
        Exit For
    End If
Next i
Exit Sub
Message:
    MsgBox "Please Enter Valid Integers as the Column Numbers Separated by Spaces."
End Sub

Note: This code produces a Macro called Delete_Rows_with_Blank_Cells.

VBA Code to Delete Row if Cell Is Blank


Code Breakdown

  • Firstly, Sub Delete_Rows_with_Blank_Cells() starts a new Macro called Delete_Rows_with_Blank_Cells.
  • Secondly, On Error Goto Message instructs the code to go to the label called Message if it finds any error while executing.
  • Column_Numbers = InputBox(“Enter the Number of the Columns with the Blank Cells: “) asks the user to input the number of the columns where the blank cells are located.
  • Then, in Code, there may be more than one such column. If there is more than one, you have to separate them by spaces.
  • Column_Numbers = Split(Column_Numbers, ” “) splits the inputs into an array of digits.
  • For example, if you input 2 3, it will split it into {2, 3}.
  • Afterward, Dim Count As Integer declares an integer called Count.
  • Here, Count=0 sets the initial value of Count as 0.
  • Then, For i = 1 To Selection.Rows.Count starts an iteration for each row of the selected range.
  • And, For j = 0 To UBound(Column_Numbers) starts an iteration for each input column of each row of the selected range.
  • After that, If Selection.Cells(i, Int(Column_Numbers(j))) = “” Then instructs that if any cell of the selected range is blank, then the next lines will be executed. Otherwise not.
  • Selection.Cells(i, 1).EntireRow.Delete instructs to delete the whole row if the cell is blank.
  • Additionally, i = i – 1 reduces the value of i by 1. It, in fact, brings the iteration one step down.
  • This is due to the fact that when any row is deleted, the total number of rows reduces by one.
  • Count = Count + 1 increases the value of the variable Count by 1 if a row is deleted. It keeps track of the number of rows been deleted.
  • Then, Exit For instructs the code to come out of the iteration through the columns of a row if any row is deleted. This is quite natural. If any cell of a row is found blank, that row will be deleted. We needn’t examine the next cells of the row.
  • End If ends the If block.
  • Next j ends the for-loop of iterating through the columns of a particular row.
  • After that, If Count >= Selection.Rows.Count Then initiates another If block, if the value of the variable Count is equal to the total number of selected rows.
  • Next, Exit For instructs the code to exit out of the for-loop of iterating through each of the rows, if the number of deleted rows becomes equal to the total number of rows. This statement is quite important. The number of deleted rows can’t be greater than the total number of rows.
  • If you don’t write these lines, you will drown within a hole of infinite for-loops.
  • Then, Exit Sub instructs the Macro to exit out of the code.
  • Message: starts a label called Message, which will be executed only if the code faces any error while running.
  • After that, MsgBox “Please Enter Valid Integers as the Column Numbers Separated by Spaces.” instructs the user to input valid numbers as the columns numbers.
  • Finally, End Sub declares the end of the Macro.


Step 4: Save Macro-Enabled Workbook

Before running the code, we need to save the workbook in the macro-enabled mode.

➤ Now, save the workbook as Excel-Macro Enabled Workbook.

Saving Micro-Enabled Workbook


Step 5: Select Data Set to Delete Row with Macro in Excel

In this step, we will select the data range where the code will be applicable.

➤ Firstly, come back to your Excel Worksheet.

➤ After that, in the worksheet, select the data set from where you want to delete the rows with blank cells (Without the Column Headers).

Selecting Data Range to Delete Row if Cell Is Blank


Step 6: Run Macro to Delete Row

Now, we will run Macro to delete rows if the cell is blank.

➤ In the following step, press ALT+F8 on your keyboard. A dialogue box called Macro will open.

➤ Then, select Delete_Rows_with_Blank_Cells (The name of the Macro) and click on Run.

Running Macro to Delete Row if Cell is Blank in Excel


Step 7: Enter Inputs in Input Box to Delete Row If Cell Is Blank

In this step, we will take input. The inputs will select the rows for deleting the rows if blank cells are found.

➤Here, you will get an Input Box asking you to enter the numbers of the columns from where you want to remove the blank cells.

➤ So, let’s delete candidates who didn’t appear in the Preliminary and Written exams.

➤Therefore, we input 2 3 (Separated by a space).

Entering Inputs in Input Box


Final Output

Now, we will get the final result.

➤ Finally, click on OK.

➤As a result, it will delete all the rows that have blank cells in columns 2 and 3.

Result After Deleting Row if Cell Is Blank Using Excel Macro

Read More: Formula to Remove Blank Rows in Excel (5 Examples)


Things to Remember

  • Here, you can use this code to delete rows with blank cells from a single column. Just enter the number of the column in the Input Box.
  • So, you can also use this code to delete the blank cells from the whole data set. Enter the numbers of all the columns of the data set. For example, 1 2 3 4.

Conclusion

In this article, we have demonstrated how to apply Excel Macro to delete the row if the cell is blank. There is a practice workbook at the beginning of the article. Go ahead and give it a try. To read similar articles, check out our website ExcelDemy. Last but not least, please use the comment section below to post any questions or make any suggestions you might have.


Related Articles

Rifat Hassan

Rifat Hassan

Hello! Welcome to my profile. Here I will be posting articles related to Microsoft Excel. I am a passionate Electrical Engineer holding a Bachelor’s degree in Electrical and Electronic Engineering from the Bangladesh University of Engineering and Technology. Besides academic studies, I always love to keep pace with the revolution in technology that the world is rushing towards day by day. I am diligent, career-oriented, and ready to cherish knowledge throughout my life.

We will be happy to hear your thoughts

Leave a reply

5 Excel Hacks You Never Knew

Genius tips to help you unlock Excel's hidden features

FREE EMAIL BONUS

ExcelDemy
Logo