Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, and 2016. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Getting Rid of Empty Rows after Importing.
by Allen Wyatt
(last updated February 24, 2018)
There are numerous times when Tom has a worksheet imported from another program into Excel so he can work with the data. The importing works fine, but the import process adds lots (dozens and sometimes hundreds) of extra rows that have no data in them. After the import Tom has to manually delete those extra rows so he can use the rest of the data. Tom wonders if there is a way to easily get rid of these empty rows.
There are a number of ways you can approach this problem. The easiest way may be to simply sort the imported data by the column of your choice. All the rows that contain nothing in that column end up at either the end or beginning of the data (depending on if you sort in ascending or descending order) and you can easily delete those rows.
Obviously, when you do a sort in this manner you could end up with your data out of the original, imported order. If you need your data to be in the original order—but with the blank rows removed—just insert a column to the left or right of your data, fill it with sequential numbers, perform the sort by any column except that added column, and then delete the rows that are blank (with only something in the numbering column). You can then sort a second time based on the numbering column and your data will be back in its original order.
Another approach is to follow these steps:
Figure 1. The Go To Special dialog box.
If you prefer to use a macro to get rid of the blank rows, you can use something similar to the following:
Sub DeleteEmptyRows() Dim LastRow As Long Dim J As Long LastRow = ActiveSheet.UsedRange.Rows.Count + _ ActiveSheet.UsedRange.Rows(1).Row - 1 Application.ScreenUpdating = False For J = LastRow To 1 Step -1 If Application.WorksheetFunction.CountA(Rows(J)) = 0 Then Rows(J).Delete End If Next J Application.ScreenUpdating = True End Sub
Why would you want to use a macro? Because you may need to delete empty rows week after week. Just put the macro into your Personal workbook and you can then access it whenever you need.
Additional information on this topic can be found on these pages:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (7988) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: Getting Rid of Empty Rows after Importing.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
When processing plain text files in a macro, it is often helpful to know how much data the file contains. The normal way ...Discover More
Excel keeps track of the most recent workbooks you've used. If you want to access that information in a macro, you'll ...Discover More
Excel provides different file formats you can use to export your worksheet information. One such file format is CSV, or ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.