VBA code to delete blank rows in range example will helps to delete empty rows in specific range from excel worksheet. We can use Delete method of Rows to delete the rows in a range. In this example we will see how to delete the rows from a range in excel worksheet using VB. Excel VBA Macro code for deleting rows in a range should work for all the version of Microsoft Excel 2003, Excel 2007, Excel 2010, and Excel 2013.
50+ Project Management Templates Pack
Excel PowerPoint Word
Advanced Project Plan & Portfolio Template
Ultimate Project Management Template
20+ Excel Project Management Pack
20+ PowerPoint Project Management Pack
10+ MS Word Project Management Pack
VBA code to delete rows in a range
Here is the Example VBA syntax and Example VBA Macro to delete rows in range from excel worksheets. This will help you to know how to delete specific rows in a range from Excel workbook using VBA.
VBA delete blank rows in range: Syntax
Following is the VBA syntax and sample VBA code to delete rows in a range from worksheet using VBA. We are using the Delete method of the Rows object of worksheet.
If Application.WorksheetFunction.CountA(Rows(RowNumber)) = 0 Then Rows(RowNumber).EntireRow.Delete
Here Application.WorksheetFunction.CountA will check if the row is blank. And the Rows.delete method will delete the row if row is blank.
Delete blank rows in Range: Examples
The following VBA code is to delete blank rows in range A10 to D20 if rows are blank.
Sub sbVBS_To_Delete_Blank_Rows_In_Range() Dim iCntr Dim rng As Range Set rng = Range("A10:D20") For iCntr = rng.Row + rng.Rows.Count - 1 To rng.Row Step -1 If Application.WorksheetFunction.CountA(Rows(iCntr)) = 0 Then Rows(iCntr).EntireRow.Delete Next End Sub
Instructions to run the VBA code to delete blank rows in range
Please follow the below steps to execute the VBA code to delete blank rows in range from Excel worksheets.
Step 1: Open any Excel workbook
Step 2: Press Alt+F11 – This will open the VBA Editor
Step 3: Insert a code module from then insert menu
Step 4: Copy the above code and paste in the code module which have inserted in the above step
Step 5: Enter some sample data in range A1 to D20 in any column. And leave some blank rows for testing purpose.
Step 6: Now press F5 to execute the code
Now you can observe that the all blank rows are deleted from worksheet in in Range A10 to D20.
: Explained VBA Code to Delete Blank Rows in Range
Start writing the Excel VBA Macro to delete blank rows in a particular range.
‘Declaring a variable iCntr as Long to store the row number iteration to use in for loop.
Dim iCntr As Long
‘Declaring range variable rng to set the range
Dim rng As Range
‘Assigning the range A10 to D20 to rng range object
Set rng = Range(“A10:D20”)
‘Looping through the rows in the range from last to frist. Here step statement will helps.
For iCntr = rng.Row + rng.Rows.Count – 1 To rng.Row Step -1
‘Checking the row if it is blank.
‘And deleting the row if it is blank.
If Application.WorksheetFunction.CountA(Rows(iCntr)) = 0 Then Rows(iCntr).EntireRow.Delete
Ending the sub procedure to delete blank rows in a range.
|Excel VBA Reference||Project Management Reference|