Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Deleting Blank Columns.

Deleting Blank Columns

by Allen Wyatt
(last updated February 23, 2017)

2

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.
  3. Click the Options button. Excel displays the Sort Options dialog box. (See Figure 1.)
  4. Figure 1. The Sort Options dialog box.

  5. Choose the Sort Left to Right radio button.
  6. Click on OK to dismiss the Sort Options dialog box.
  7. Using the first Sort By drop-down list, specify the row by which you want to sort.
  8. 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.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2660) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Deleting Blank Columns.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Toggling Font Assignments in a Macro

If you need to quickly switch a text selection from one typeface to another, one way you can do it is with a macro. This tip ...

Discover More

Absolutely Getting Rid of Formatting

Need to get rid of the formatting applied to a bunch of text? One of the easiest ways to do this is to use Notepad in ...

Discover More

Understanding Page Sizes

When you create a document, you need to be concerned about the final size of the page you will be creating. Word supports a ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

MORE EXCELTIPS (MENU)

Deleting Everything Except Formulas

Need to get rid of everything in a worksheet except the formulas? It's easier to make this huge change than you think it is.

Discover More

Conditionally Deleting Rows

Want to delete a bunch of rows in a worksheet based on the value in a certain cell of each row? There are a couple of ways ...

Discover More

Deleting Every X Rows without a Macro

Grab some info from a source other than Excel, and you may find the need to delete a certain pattern of rows from a ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments for this tip:

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is eight more than 7?

2014-05-29 10:24:44

Dave

Hi sushama.

I have written a macro for you, which will do what you are looking for. Please note the following "conditions":

1. Your data is expected to be in a grid of 8 columns and 40 rows. The grid can be anywhere on the sheet.

2. You MUST position the cursor at the top left cell of the grid.

3. Before you run the macro, you need to find a column that has no data. Insert the column NUMBER (NOT the letter) in the code so that it is assigned to the variable SC (sorting column). I have used 150, which is beyond the range of most spreadsheets!

Two steps to the process:
1. Copy the data to a single column, but only the non-empty cells.
2. Write the new column of cells back to the original grid.
In both cases, clear the cells as you go.

I hope you find this useful. Here is the code.

Dave

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

Sub ShiftCells()
Dim x As Integer ' top left corner row
Dim y As Integer ' top left corner column
Dim i As Integer ' loop counter
Dim j As Integer ' loop counter
Dim MaxX As Integer ' max row
Dim MaxY As Integer ' max column
Dim a As Integer ' counter for new column
Dim SC As Integer ' sort column

x = Selection.Row
y = Selection.Column
a = 1
SC = 150

MaxX = x + 39
MaxY = y + 7

' write the cell values to a new column
For j = y To MaxY
For i = x To MaxX
If Cells(i, j) <> "" Then ' if the cell has a value then
Cells(a, SC) = Cells(i, j) ' copy the value
a = a + 1
End If
Cells(i, j) = "" ' clear the original cell
Next i
Next j

' write the corrected list back
a = 1
For j = y To MaxY
For i = x To MaxX
If Cells(a, SC) <> "" Then
Cells(i, j) = Cells(a, SC) ' copy the values
Cells(a, SC) = "" ' clear the corrected cells
a = a + 1
Else
Exit For
End If
Next i
Next j

End Sub


2013-04-18 01:02:13

sushama

We have listed book no. in excel sheet in ascending order for stock verification. Some of the books have been weeded out, which leaves blank cells in the sheet. So how to delete these cells without disturbing the sequential order. The no. are in 8 columns and 40 rows in every sheet. i.e.41 no. comes on the second colum and so on.. Pl. guide


Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.

Links and Sharing
Share