bottom
Great ExcelTips!
         
Your e-mail address is safe!
Close Note

Tips.Net > ExcelTips Home > Tools > Converting Imported Information to Numeric Values

Converting Imported Information to Numeric Values

Summary: When importing data into Excel that was created in a different program, the results you get are dependant on the characteristics of the data. If numeric values are not in a standard form, then they may be treated as text. This tip explains a few ways you can overcome the problem of non-standard formats for incoming numeric data. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, and Excel 2003.)

Subscriber Garrett Kolo asked if there was a way to quickly convert text data to numerical data. He is importing a text file that uses spaces in the thousands place (1 256) instead of a comma (1,256).

There are several ways to approach this problem. The first is to understand the source of the problem. The text file is probably created on a system that is following a metric standard. Some countries, following the metric standard, use a space for a thousands separator instead of a comma. Thus, you could import the file properly into Excel if you change your regional settings in Windows before starting Excel and doing the import. You can change the regional settings by using the Regional Options applet in the Control Panel.

If you don't want to change the regional settings on your system, there are other approaches you can take. After Excel imports the information, you can select the range of cells that contain numbers and simply do a search and replace. You are searching for a single space and replacing it with nothing. This does away with the space completely, and Excel will then treat the contents of the cell as a number.

You can also use a formula, if desired, to modify the imported data. For instance, if the imported number (containing a space) is in cell A3, you could use this formula to strip out the space:

=1*SUBSTITUTE(A3," ","")

Note that there is a space between the first set of quotes and nothing between the second set of quotes.

If you have quite a bit of data to convert, or if you have text interspersed with the "numbers-only" cells, then you may decide to use a macro to do the conversion. The following macro works on a selection you make before calling it. It also checks to make sure that the cell--after removing the spaces--contains a numeric value. If it doesn't, then no conversion is done.

Sub ClearSpaceIfNumeric() 'Removes spaces from cell values if they are otherwise numeric, 'transforming them from text to actual numbers. Dim c As Range 'Cell under examination Dim tmpText As String 'Cell contents without spaces Dim i As Integer 'Simple counter For Each c In Selection 'Go through every cell in the selection tmpText = "" 'Initialize 'Check each character to see whether it's a space. 'If it isn't, then add it to tmpText, otherwise ignore it For i = 1 To Len(c.Text) If Mid(c.Text, i, 1) <> " " Then tmpText = tmpText & Mid(c.Text, i, 1) End If Next i 'tmpText is now the cell contents without spaces 'If tmpText is a number, then assign its value to 'the current cell If IsNumeric(tmpText) Then c.Value = tmpText End If Next c End Sub

Tip #2248 applies to Microsoft Excel versions: 97 | 2000 | 2002 | 2003


Step Up and Take Control! Subscribers to ExcelTips know just how valuable a resource it is. ExcelTips Premium provides twice the number of exceptional, easy-to-understand tips every week in an ad-free newsletter, as well as substantial discounts on ExcelTips archives and e-books.
 
Check out ExcelTips Premium today!

Helpful Links

Ask an Excel Question
Make a Comment

Tips.Net Home
Vital News Home

ExcelTips FAQ
ExcelTips Premium

Learn Access Now

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

 

Great Info!

Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your e-mail address and click "Subscribe."
     
(Your e-mail address will never be shared with anyone, ever.)