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.
Written by Allen Wyatt (last updated December 27, 2021)
This tip applies to Excel 2007, 2010, 2013, and 2016
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:
http://www.cpearson.com/Excel/deleting.htm#DeleteBlankRows http://dmcritchie.mvps.org/excel/lastcell.htm
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.
Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!
Does your macro need to add information to the end of a text file? This is called appending and is done using the ...
Discover MoreYou can use Excel for all types of data processing. You may want to work with filenames in a worksheet, but the first ...
Discover MoreWhen you import information from a CSV text file, Excel formats the data according to its default settings. Wouldn't it ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2021-12-27 09:18:59
Mark W
David Robinson's point that incoming data with unexpected results should get your spider senses tingling until it can be verified as correct is excellent. If the data is indeed correct I submit that this scenario is a perfect case for using Power Query. This would also address Alex B's points as well.
2018-02-26 09:55:13
David Robinson
Another note of caution is to say that my spider-senses always detect trouble when the size of a data table isn't what I expected, and if your data filled the range you copied but doesn't fill the range when it's pasted, then you've probably got some formulas that evaluate in their home context but no longer produce a value in the context where you paste them. In that case you should consider pasting as values.
2018-02-24 09:30:43
Mark
Fellow Keyboard Jockeys:
And for all those who also prefer keyboard shortcuts you can use Ctrl - (control minus) to delete selected rows. This works for the first two methods.
2018-02-24 07:54:25
Alex B
A note of caution when you use the first method. When you enter the sequential number to reinstate the original order you need to use the Home > Fill command to generate the numbers. If you use a formula ie =a1+1 you must paste values prior to the sort (the number needs to be a constant)
Typically downloads contain not only blanks but also repeating page headers and totals that you want to remove. Sorting the rows generally lets you delete all of these in one go.
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments