Tuesday, September 1, 2009

Deleting Blank Columns

FOR SOURCE CLICK HERE
Summary: Import data from another program, and you could end up with a lot of blank columns in your data. Here's the quickest way to get rid of these empty columns. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, Excel 2003, and Excel 2007.)

When importing information from an external source, it is possible that the data will contain blank columns—columns with nothing in them. If you import a lot of data, then deleting these columns can be a bother. There are a couple of ways you can approach how to delete these columns.

The first approach works very well if your data is sorted by column. In other words, the data that you import is in ascending order, or you want it in sorted order. In this case, follow these steps:

1. Select the columns that represent your data. Make sure you select, as part of the range, all the blank columns as well.
2. Choose the Sort option from the Data menu. Excel displays the Sort dialog box. (To display the dialog box in Excel 2007, display the Data tab of the ribbon and then click Sort.)
3. Click the Options button. Excel displays the Sort Options dialog box. (Click here to see a related figure.)
4. Choose the Sort Left to Right radio button.
5. Click on OK to dismiss the Sort Options dialog box.
6. Using the first Sort By drop-down list, specify the row by which you want to sort.
7. Click on OK.

When sorting in this manner, all the empty columns end up "pushed" to the right, and your data is in a sorted order.

If you don't want your data sorted, then you can use a nifty macro that will check for blank columns in a selected range and then delete those columns. The following macro will do the trick:

Sub DeleteEmptyColumns()
first = Selection.Column
last = Selection.Columns(Selection.Columns.Count).Column
For i = last To first Step -1
If WorksheetFunction.CountBlank(ActiveSheet.Columns(i)) = 65536 Then
Columns(i).Delete
End If
Next i
End Sub

To use the macro, select the range of columns in which you want blank columns deleted. The macro steps through the columns and if the column is truly blank, it is deleted. You should note that this macro will delete only columns that are truly empty. If cells within a column include a formula that returns a zero value (and you have the display of zeros values turned off) or that returns an empty string, then the column isn't empty—it contains formulas. In this case, the column won't be deleted.

No comments:

Post a Comment