Delete blank rows in a table using VBA

Home/VBA/Delete blank rows in a table using VBA

VBA code to delete blank rows in table example will helps to delete empty rows in specific table range from excel worksheet. We can use Delete method of Rows to delete the rows in a table. In this example we will see how to delete the rows from a table in excel worksheet using VB. Excel VBA Macro code for deleting rows in a table should work for all the version of Microsoft Excel 2003, Excel 2007, Excel 2010, and Excel 2013.

PREMIUM TEMPLATES LIMITED TIME OFFER

ON SALE80% OFF

50+ Project Management Templates Pack
Excel PowerPoint Word

Advanced Project Plan & Portfolio Template
Excel Template

Business Presentations Templates Pack
PowerPoint Slides

20+ Excel Project Management Pack
Excel Templates

20+ PowerPoint Project Management Pack
PowerPoint Templates

10+ MS Word Project Management Pack
Word Templates


VBA code to delete rows in a table


Here is the Example VBA syntax and Example VBA Macro to delete rows in table from excel worksheets. This will help you to know how to delete specific rows in a table from Excel workbook using VBA.

VBA delete blank rows in table: Syntax


Following is the VBA syntax and sample VBA code to delete rows in a table 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 Table: Examples


The following VBA code is to delete blank rows in Table1 in the active sheet if rows are blank.

Sub sbVBS_To_Delete_Blank_Rows_In_Table()
Dim iCntr As Long
Dim rng As Range
Set rng = ActiveSheet.ListObjects("Table1").Range
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 table


Please follow the below steps to execute the VBA code to delete blank rows in Table1 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 and insert table. 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 Table1.

Explained VBA Code to Delete Rows in Table


‘Start writing the Excel VBA Macro to delete blank rows in a particular range
Sub sbVBS_To_Delete_Blank_Rows_In_Table()

‘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 = ActiveSheet.ListObjects(“Table1”).Range

‘Looping through the rows in the table tange 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
Next

End Sub
Ending the sub procedure to delete blank rows in a table range.

LIMITED TIME OFFER
By |April 21st, 2014|VBA|2 Comments

About the Author:

PNRao is a passionate business analyst and having close to 10 years of experience in Data Mining, Data Analysis and Application Development. This blog is his passion to learn new skills and share his knowledge to make you expertise in Data Analysis (Excel, VBA, SQL, SAS, Statistical Methods, Market Research Methodologies and Data Analysis Techniques).

2 Comments

  1. rd November 13, 2014 at 1:39 PM - Reply

    great article. I needed a refresher. Thanks..

  2. Al K February 20, 2015 at 1:58 AM - Reply

    My VBA Editor shows that

    For iCntr = rng.Row + rng.Rows.Count – 1 To rng.Row Step -1

    has Compile Error: Syntax error

Leave A Comment


Related pages


print macro excelcreating pivot charts in excel 2010vba connection string to access databaseunhide first row in excelexcel formulas explainedremoving duplicate rows in excelpowerpoint dashboard templatevbcancelhow to change column width in excel 2010excel count duplicatems excel shortcuts keys listvba select case truevba status bar progressexcel formula operatorsconstruction cost excel templatehow to sort duplicates in excel 2010project plan template xlsfor loop in vba excelvba sql query excelsql ddl and dmldeveloper tab excel 2013excel formula functions and examplesms excel practical questionsauto hyperlink in excelexcel vba dialog boxtricky interview questions and answers for freshersvba clearcontentsdml ddl dcl and tcl in sqlhow to make excel sheet password protectedinsert blank rows in excelusing match function in excelvlookup fixed rangeexample of countif in excelvba code to copy data from one sheet to anothercreating a userform in excelsql interview questions and answers pdf for experiencedmake drop down list in excel 2007vba replace methodexcel vlookup function tutorialexcel export to txtexcel chart tutorialvba case switchexcel vba select columnhow to use iferror function with vlookup in excel 2010conditional logic excelvisual basic programing examplesvba open excel applicationvba combobox selected itemexcel 2007 macrosmicrosoft excel protect sheetvba picturedelete row in excel vbalistbox itemsproc sql tutorialexcel sheet shortcut keysvb basic codesvlookup formula with examplevba close excelrun vba codemsdasql connection stringexcel template checklisthow to unprotect excel cellscreating drop down menus in excel 2007vba combobox codeexcel macro copy row to another sheetexcel hard returnvba message box exampleloop function in excelhow do you delete rows in excelsql sas