• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Microsoft Excel Tips and Tricks - Computergaga

  • Home
  • Excel Online Courses
  • Excel Functions List
  • Free Downloads
  • Blog
  • Tutorials
  • About
Home / VBA / Excel Macros Tutorial – Remove Blank Rows Macro

Excel Macros Tutorial – Remove Blank Rows Macro

Blank rows in your Excel spreadsheet are undesirable and can make it difficult to work with. Because of this, Excel provides many ways that we can locate and then delete the blank rows.

These techniques can include sorting the list to group the blank rows together, filtering for blanks and using Go To Special to locate blank cells for deleting.

However these can be time consuming and may not be the best approach for you. For example, sorting the list will change the order of the list. You may not want this.

By creating a macro to delete the blank rows of a spreadsheet this process will be effortless, and can also be performed by anyone when the macro is made available with a button. In this Excel Macros tutorial we show you how.

To create the delete blank rows in a spreadsheet macro;

  1. Open the Visual Basic Editor by pressing Alt + F11
  2. Insert a module by clicking the Insert menu and selecting Module
  3. Enter or copy and paste the code below into the window

In the code below comments are used (shown in green) to explain each section of the code.

Sub RemoveRows()
Dim lastrow As Long
Dim ISEmpty As Long
‘Count how many records in the list. This is done so that the Do loop has a finish point.
lastrow = Application.CountA(Range(“A:A”))
‘Start at the top of the list
Range(“A1”).Select
‘Loop until the end of the list
Do While ActiveCell.Row < lastrow
‘Assign number of non empty cells in the row
ISEmpty = Application.CountA(ActiveCell.EntireRow)
‘If ISEmpty = 0 then delete the row, if not move down a cell into the next row
If ISEmpty = 0 Then
ActiveCell.EntireRow.Delete
Else
ActiveCell.Offset(1, 0).Select
End If
Loop
End Sub

If you enjoyed this Excel macros tutorial then check out our online Excel VBA course to fast track your macro writing skills.

Related Posts:

  • Calculate age in Excel thumbnail
    How to Calculate Age in Excel
  • Moving average in Excel thumbnail
    Moving Average in Excel
  • Case sensitive XLOOKUP feature
    Case Sensitive Lookup with XLOOKUP in Excel
  • Sum formulas only in Excel
    Sum Formulas Only in Excel

Reader Interactions

Comments

  1. Brenda says

    12 July 2013 at 6:41 pm

    Thanks for this. It works great for up to 1826 rows, but I would like it to work for a spreadsheet that is over 15,000 rows. It just stops at 1826 rows for some reason.

    Reply
    • computergaga_blog says

      14 July 2013 at 3:32 pm

      Hi Brenda,
      I cannot explain this. I just tested it on over 5000 rows quickly and it worked fine. It should work on any number of rows, although obviously many will run slower.

      Reply
  2. Stan Czart says

    13 July 2013 at 3:38 pm

    Hello Allan. I believe there is an error in your code. For last row, your formula is counting how many non-blanks instead of how many rows have data. I ran your procedure and it stopped before deleting all the blank rows. I think you have to start from the bottom and use the row number of the last cell that is not blank to properly calculate the last row variable.
    Stan

    Reply
    • computergaga_blog says

      14 July 2013 at 3:34 pm

      Hi Stan,
      Thank you for your message, but to delete all the blanks the formula is correct to count all the non blanks rows. This is when to stop looping so as the rows are removed this will be the finished total.
      If we grabbed the last row by starting from the bottom. The loop would continue as it keeps trying to remove rows to get to that number.

      Reply
  3. AK says

    10 October 2013 at 1:56 am

    great macro…works by deleting stuff if you have nothing in column A but if you want it to work for the whole sheet ….make one nimor change
    Change
    lastrow = Application.CountA(Range(“A:A”))
    To
    lastrow = Application.CountA(Range(“A:XFD”))

    Reply
    • computergaga_blog says

      10 October 2013 at 6:23 am

      A good suggestion AK.

      Reply
  4. Rocky says

    12 October 2013 at 6:39 pm

    Hello Allan,
    I have entered code into the window as explained above and i saved the file but it doesn’t effecting my Excel data to remove blank rows . may i know next step to execute macro.
    -thank you

    Reply
    • computergaga_blog says

      16 October 2013 at 8:01 am

      You will need a button or keyboard shortcut to execute the macro.

      Reply
  5. Mike382P says

    8 November 2013 at 4:37 am

    Excellent topic! I certainly agree that empty rows/cells can cause undesirable results. However, I do think the code should be changed:
    1. Using lastrow = CountA (…) function will not always give you the last row. For example if there is an entire row between your data that is blank, the count will be off.
    For the suggestion of using CountA(“A:XFD”), that also probably will not work as desired for getting the last row. For example, you have 1 row and data in column A,B and C. Using the CountA(“A:XFD”) function, that will make lastrow = 3. (And if there are more cells with data than rows, an error could occur when trying to move to that row).
    2. Selecting cells can be slow for large amounts of data – generally it’s not necessary to Select cells. It’s not too bad, just usually not necessary.
    The below might not be the best solution, but should be a step closer.
    Public Sub RemoveRows()
    Dim lastrow As Long
    Dim CurrentRow As Long
    CurrentRow = 1
    lastrow = Range(“A1”).SpecialCells(xlCellTypeLastCell).Row
    Do While CurrentRow <= lastrow
    If 0 = Application.CountA(Range("A" & CurrentRow).EntireRow) Then
    Range("A" & CurrentRow).EntireRow.Delete
    lastrow = lastrow – 1
    Else
    CurrentRow = CurrentRow + 1
    End If
    Loop
    End Sub

    Reply
    • computergaga_blog says

      8 November 2013 at 7:35 am

      Thank you for your comment Mike. Yes there are always multiple ways of achieving a goal with a macro. I write many posts regarding the use of the Cells object and alternatives that are faster than selecting cells in large workbooks, or large macros. However the right one is the one that works and people are comfortable in creating.
      On point 1 the CountA is fine because I don’t want to count the blank rows. It is correct in what it is doing because it needs to know when to finish and as the blank rows are being removed, that will be the last row when done, although not when started.

      Reply
    • Mary says

      24 February 2015 at 7:42 pm

      Thanks for the modify code. The original did not work for me. However, your modifications worked on deleting my blank rows.
      Many Thanks!!!

      Reply
  6. Dell says

    13 September 2016 at 10:28 pm

    I keep getting a ‘Comple error: Syntax error’
    I’m new to this so be gentle!

    Reply
    • computergaga says

      16 September 2016 at 6:44 am

      🙂
      Syntax error will probably mean that you have made an innocent mistype or misspelling. Double check the code.
      Hard for me to tell further without seeing the spreadsheet you are using a code.

      Reply
      • Dell says

        26 September 2016 at 3:51 pm

        Thanks. I copied your code to my Visual Basic Editor. I’m sure it’s me. I’m not used to doing this kind of thing.

        Reply
        • computergaga says

          27 September 2016 at 9:30 pm

          Sorry Dell it is hard to help without knowing more. All I can ask is if you check the video and just cross reference the two.
          Does it highlight a particular line in the code when showing the error?
          It may not be the code, check the spreadsheet is laid out like mine. You may have a different cell range to my example.

          Reply
          • Steven says

            7 September 2017 at 9:13 pm

            I had the same issue. What is highlighted is Sub RemoveRows()

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Recent Posts

  • Excel SWITCH Function – Nested IF Function Alternative
  • Sum Formulas Only in Excel
  • How to Calculate Age in Excel
  • Custom Sort in Excel
  • Case Sensitive Lookup with XLOOKUP in Excel
Advanced Excel Success book

Footer

Contact

[email protected]

LinkedIn Twitter YouTube

Useful Links

  • Home
  • Excel Online Courses
  • Tutorials
  • Sitemap
  • Privacy Policy
  • Jobs by Jooble

Course Topics

  • Blog
  • Charts
  • Courses
  • Dashboards
  • Downloads
  • Excel Tips
  • Formulas
  • Mail Merge
  • Office
  • PivotTables
  • Power BI
  • Power Pivot
  • Power Query
  • VBA

Join Our Mailing List

© Copyright 2009 Computergaga · All Rights Reserved ·