
Tips.Net > ExcelTips Home > Editing > Getting Rid of Spaces in Cells
Summary: Getting rid of stray spaces that clutter up otherwise empty cells can be a bother. This tip explains how you can use a couple of different techniques to make the task easier than it might otherwise appear. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, and Excel 2003.)
Carole imports information into Excel from a different program, and this often leaves extra spaces in some cells. The spaces are the only things in the cells, so they appear to be empty but really aren't. Carole wondered about the best way to get rid of these unnecessary spaces.
There are a couple of approaches you can use. The first is to use the Find and Replace capabilities of Excel. Follow these steps:
Choose Replace from the Edit menu. Excel displays the
Another option is to use the Trim worksheet function. This approach is handy if the cells you want to modify are all in a particular area of the worksheet, such as a single column. For instance, if you want to get rid of the spaces from the cells in column D, you could use the following formula:
=Trim(D1)
The Trim function returns the contents of cell D1 without any leading or trailing spaces. You could then copy the results of this formula and use Paste Special to paste the values back into whatever cells you desire.
Of course, if you have lots of worksheets you need to process, or if you routinely get workbooks that contain the extra spaces in cells, a better way would be to create a macro that could get rid of the spaces. Perhaps the fastest way would be to examine all the cells in the worksheet and get rid of any extra spaces:
Sub CleanSheet1()
For Each cell In ActiveSheet.UsedRange
cell.Value = Trim(cell)
Next cell
End Sub
The macro steps through each cell and uses the Trim function to get rid of any leading or trailing spaces. This works on all the cells, but it may produce undesired results, depending on the characteristics of your data. If you have cells that have leading spaces--and you want those spaces--then you'll need to use a different macro. This version will give more satisfactory results:
Sub CleanSheet2()
Dim rCell As Range
Dim rText As Range
Set rText = Cells.SpecialCells( _
xlCellTypeConstants, _
xlTextValues)
For Each rCell In rText
If Trim(rCell.Value) = "" Then
rCell.ClearContents
End If
Next
Set rText = Nothing
Set rCell = Nothing
End Sub
It only checks those cells containing constants (which includes all text in the worksheet) and then checks to see if using the Trim function would result in an empty cell. If so, then the cell is cleared. If the Trim function wouldn't result in an empty cell, then no change is made to the cell.
This macro could be added to your Personal.xls workbook so that it is available whenever you have Excel open.
Tip #2883 applies to Microsoft Excel versions: 97 2000 2002 2003
Make Home Buying Less Stressful! Why face the annual problems associated with Christmas debt? Learn how to avoid that debt and conquer your financial challenges.
Check out Buying a Home Checklist today!
No, not that type of date. If you need to do any types of work with calendar dates, Excel has the tools you need. Learn how to use those tools the easy way. (more information...)
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site