Delete blank rows in a range using VBA

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

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.

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 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.
Sub sbVBS_To_Delete_Blank_Rows_In_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
Next

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

LIMITED TIME OFFER
By |April 21st, 2014|VBA|6 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).

6 Comments

  1. D K Mishra July 15, 2015 at 9:30 AM - Reply

    Thanks Dr.Rao.
    After I identified your site, my first search has been analyststabs for a simple understandable solution and implement in my codes. Thanks a lot again for your nice guidance and help. D K Mishra, SAIL, Rourkela Steel Plant

    • PNRao July 15, 2015 at 7:53 PM - Reply

      Glad you found our site useful! Thanks-PNRao!

  2. September 16, 2015 at 11:34 AM - Reply

    Hi PNRao

    I’m lost, I have a macro taking the values from a invoice (invoice number, name, item, amount etc) and adding it to a running total on a separate sheet called ‘Invoice Data’

    But due to a limitation I have 20 possible entries on the invoice for items, when I run my macro to save the data into “Invoice Data” it transposes all the remaining rows that have no data in it.

    Is there a way to remove the empty cells from being transposed to the second sheet?

    The reason why it comes up is the customer name and invoice are replicated for each line

    • PNRao September 16, 2015 at 10:48 PM - Reply

      Hi,

      Could you please provide an example file with sample data, so that I can write macro and send it to you. Please email to:

      Thanks-PNRao!

  3. Dider Elahi September 24, 2015 at 10:40 AM - Reply

    Sir, from ur formula it only remove A1:D10 blank data, But I want delete data from my blank cell for that reason I send a sample file for filtering/ deleting blank data. If it possible pls help me.

  4. khirod kumar March 29, 2016 at 10:49 AM - Reply

    Please any one can send me basic VBA book in my email I’d

Leave A Comment


Related pages


vba listbox columnvbs write fileinsert rows in excel shortcutformula to remove special characters in excelexcel based dashboardexcel vba save as csvsimple macros in excelsimple vlookup in excel 2010excel chraptitude test on exceluseful vba functionshow to adjust the column width in excelmerge a cell in excelvba range clearexcel xml vbagoto vbapowerpoint swot analysis templateexample of vlookup function in excelhow do i remove blank rows in excelword vba programmingprint in vbahow to open excel workbookhow to use nested if function in excel 2007excel deduplicationexcel vba delete pivot tableautofilter vba criteriaprogrammer analyst interview questionssample dashboard reportsunprotect sheet without passwordsql copy recordhow to write a vbscript fileexcel vba tutorexcel vba expertunprotect workbook excel 2013how do i unhide a column in excel 2010meeting minute templatevba programming languagehow to unprotect excel sheet 2007how do you autofit a column in excelexcel color index numbersexcel chart add inspie chart excel templateinsert a checkbox in excelhow to create excel macroclustered cylinder chart excelexcel pull data from another sheetcopy excel sheet with formulas to another workbookmessage box excel vbaexcel gotosample vlookuphow to combine multiple excel sheets into one sheetsum vlookupvba excel stringexcel macro enabled worksheetrtrim vbausing sumif excelvba rgb color chartvba lessonspivot table basicsexcel merge cells formularecording meeting minutes templatedelete duplicate cellssample sql interview questionsdelete excel worksheetexcel vba insert rowsmsgbox in macrovbyesnolocate duplicates in excelvba excel save workbookplot chart in excelselect cell vbamacros in excel examplesexcel macro hide rowms access sql server connection stringuserform listbox