cmiles – blog

Charles Miles – Tucson Hiking, Outside and Adventures, Excel, .net, Life

Excel – ‘Number Stored as Text’

I frequently encounter Excel Worksheets with Smart Tags scattered around wanting very badly to share their ‘Number Stored as Text’warning. Sometimes this warning is completely incorrect – the cell value really is a string even though it looks like a number (UPCs and US Zip Codes come to mind); sometimes the cell value really should be should be a number…

The problem with numeric values identified as ‘Numbers Stored as Text’is that they can cause trouble with formulas, sorting and PivotTables – a broad enough range of activities/items to cause problems for just about anyone using the data.

Unfortunately simply changing the number format or alignment of the cells involved does not solve the problem. I like the information in the links below – they cover most of the helpful answers/suggestions that I have seen:
Daily Dose of Excel – Number Stored as Text (the post AND comments are good reading)
Jim’s Help Pages – Problems with imported data (also very good reading: Excel KB articles)

Notes On Solutions:
[please see the comments below from Gary Bouwman for some interesting information about these solutions and working in other languages]


‘Number Stored as Text’Smart Tag Menu
-
This works, but there are often better solutions. If you only have a few cells to convert the context menu from the Smart Tag may work for you (and does not require any code) – but with large ranges the conversion process can be slower than some of the methods detailed below. (Selection hint: When selecting ranges that you want to convert via the Smart Tag Menu make sure that the Smart Tag comes up on the first cell you select. If the start of your selection is a ‘normal’cell the Smart Tag will not appear, even if your final selection includes cells that trigger the Smart Tag) (Note: Smart Tags will not appear in older versions of Excel!)

Formulas
-
The links above mention the use of formulas – I think that Paste Special is usually a better option. Paste Special does not require you to find room for an extra row/column for your formulas – and (depending on your needs) does not require extra effort/key strokes to get the final version of the data into the correct position on the sheet or converted from formulas into values.

Paste Special
-
The links above detail using paste special – a very good solution! Paste Special is available directly in the UI and is quite fast and easy – in code it can also be a good solution. The Daily Dose of Excel article specifically recommends the combination of ‘Copy Blank Cell/Paste Special/Add’. The ‘Copy Blank Cell/Paste Special/Add’combination usually is the best – ‘Copy Cell with Value of 1/Paste Special/Multiply’is also effective but can convert blank cells to zeros which is a problem in some data. (Side Note: In code I dislike needing to find a blank cell to copy before the paste special, but I have NEVER worked with a sheet that has every cell filled so it would not be hard to find a blank cell – this is purely a matter of taste…)

cell.Value = CDbl(Cell.Value) -
This style of coding (which could be any number of conversions such as CInt) does the job – but I have found it to be slow with large amounts of data.

[Range].Value = [Range].Value -
This solution is simple, fast and usually a very good option. I love the simplicity of this code – unfortunately it does not work on one of the reports I frequently use. I have not read about other people having failures – but for me [Range].Value = [Range].Value fails consistently on data I need to use. Because of the problems I have had I tend to use TextToColumns (which I have not (yet) seen fail).

TextToColumns -
This is an interesting method that runs quite quickly. TextToColumns works on a single column at a time and is a decent solution both from code and through the UI. The heart of the vb.net code that I use is below. This code is much more complex than [Range].Value = [Range].Value and the range that can be used is limited to a continuous selection in a single column – but for me TextToColumns has proven to be more robust than [Range].Value = [Range].Value, faster than [CellRange].Value = CDbl([CellRange].Value) and convenient since I am usually dealing with entire columns of a table.

This code block needs two variables defined: rangeToConvert (an Excel.Range that must be a continuous selection in a single column) and Delimiter (String).

    Dim foundPreExistingDelimiter As Excel.Range = _
      rangeToConvert.Find(What:=Delimiter, _
        After:=rangeToConvert.Cells(1, 1), _
        LookIn:=Excel.XlFindLookIn.xlValues, _
        LookAt:=Excel.XlLookAt.xlPart, _
        SearchOrder:=Excel.XlSearchOrder.xlByRows, _
        SearchDirection:=Excel.XlSearchDirection.xlNext, _
        MatchCase:=False)
 
    If foundPreExistingDelimiter IsNot Nothing Then
      Throw New System.ArgumentException( _
      "Tab Delimiter used in the TextToColumns function " & _
      "is found in the Range to Convert.")
    End If
 
    'The optional Fiedinfo:= is ommitted, I could not think of 
    'a use beyond the 'general' format since this is meant to 
    'eliminate Numbers Stored as Text rather than wrap 
    'TextToColumns()
 
    rangeToConvert.TextToColumns(Destination:=rangeToConvert, _
      DataType:=Excel.XlTextParsingType.xlDelimited, _
      TextQualifier:=Excel.XlTextQualifier.xlTextQualifierNone, _
      ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False, _
      Comma:=False, Space:=False, Other:=False, OtherChar:=Delimiter, _
      TrailingMinusNumbers:=True)

TextToColumns could be a real mess if the parameters given to TextToColumns cause some of your cells to be split into multiple values (the main purpose of this function after all…) – I decided to deal with this potential problem by coding the delimiter character as a variable and checking to see if it exists in the rangeToConvert. If the delimiter is found an exception is thrown and the conversion is stopped (from a calling routine the exception makes it easy to wrap the conversion in a try-catch block and surround it with a For-Each loop that runs thru a list of possible delimiter characters).

Hope this was useful -
CM

Advertisement

Filed under: .net, Excel

11 Responses

  1. erik says:

    Simple code to provide for range.value=range.value
    Works very fast since it only takes the used cell for re-evaluation

    Sub valueISvalue()

    ‘ valueISvalue Macro
    ‘ Macro recorded 25-09-2006 by Erik Esveld


    For Each mycell In ActiveSheet.UsedRange
    mycell.Value = mycell.Value
    Next
    End Sub

  2. cmiles says:

    erik – I like .UsedRange as well (although a quick search will reveal that many people have found it to be troublesome in some situations and use other methods to get the ‘used range’) – you should be able to simplify your code by just using:

    Sub valueISvalue()

    Activesheet.UsedRange.Value = _
    Activesheet.UsedRange.Value

    End Sub

    [range].value = [range].value methods are quite fast – although (as I mentioned above) I do have one report (from a web interface) for which this method does not work.

    CAM

  3. Gary Bouwman says:

    I’ve tried several of the methods you refer to with some success. Your example for text-to-column is the only method that has worked for me so far. My particular issue is coding for use in a global environment. These simple methods all worked when my PC was set to US (English), but not when set to Germany (German) … until the text-to-column trick. Great workaround. I would never have thought to look at that as a fix.

    Gary

  4. Gary Bouwman says:

    OK. It was a great solution, but I’ve found a situation that does not work. When the PC region is set to French, texttocolumn converts the cell to a value as expected, with on exception. When the cell was already evaluated as numeric (no green triangles), it replaces the comma with a period for the decimal, and that cell is now text (with a green triangle).
    What bugs me is this texttoolumn solution works well for other language regions.

  5. cmiles says:

    Gary – really interesting comment about the French, I have not seen that mentioned before and that it would have bugged me too to find out about the , to . switch. If you find a work around let me know, I will have to experiment more with other languages…

    Charles

  6. perplexed says:

    I am programatically creating a csv file for export to other systems. Some things that need to be exported are defined as character and can be up to 50 characters long. If a user happens to fill those fields with numerics, Excel converts them to numeric representation such as 1.71717171717171E+29. I can not expect my users to convert all of this before using the file. How can I keep this from happening?

  7. Mary says:

    I have tried the paste special using either add 0 or multipy 1. Doesn’t work.

    Text to Columns doesn’t work.

    And copy and paste special – valuse only doesn’t work.

    I have no knowledge of macroes or vb.

    Any suggestions?

  8. Renee says:

    I am using the Text to Columns method and it seems there is a bug. If the topmost row(s) is not populated and the entire column is selected, the pasted cells jump up a few rows – sometimes 4 rows, sometimes 2.

    ??????

  9. Josh Hardin says:

    This is how it’s done, for most normal situations (ie, not French users).

    Public Sub ConvertNumbersStoredAsText(WSName$)
    Application.EnableEvents = False ‘prevent triggering event macros
    Dim iCell
    With ActiveWorkbook.Sheets(WSName)
    For Each iCell In .UsedRange.SpecialCells(xlCellTypeConstants, xlTextValues).Cells
    If iCell.Errors.Item(xlNumberAsText).Value Then
    If (Left(iCell.Text, 1) “0″) Then iCell.Value = iCell.Value ‘skip cells with leading zeros
    End If
    Next
    End With
    Application.EnableEvents = True
    End Sub

  10. Josh Hardin says:

    For some reason the ‘not equals’ operator was lost when my post was translated to HTML. The line containing ‘iCell.Value = iCell.Value’ should have the ‘not equals’ operator before the “0″ in the ‘if’ statement.

  11. [...] #split {}#single {}#splitalign {margin-left: auto; margin-right: auto;}#singlealign {margin-left: auto; margin-right: auto;}.linkboxtext {line-height: 1.4em;}.linkboxcontainer {padding: 7px 7px 7px 7px;background-color:#eeeeee;border-color:#000000;border-width:0px; border-style:solid;}.linkboxdisplay {padding: 7px 7px 7px 7px;}.linkboxdisplay td {text-align: center;}.linkboxdisplay a:link {text-decoration: none;}.linkboxdisplay a:hover {text-decoration: underline;} function opensingledropdown() { document.getElementById('singletablelinks').style.display = ''; document.getElementById('singlemouse').style.display = 'none'; } function closesingledropdown() { document.getElementById('singletablelinks').style.display = 'none'; document.getElementById('singlemouse').style.display = ''; } Best Music of 2009 List.Excel – ‘Number Stored as Text’ [...]

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

twitter -> twitterings

  • Just saw the new Resharper 6.1 Early Access version has Async CTP support listed - downloading now... 2 months ago
  • Quick post about FeedDemon/Pinboard/Save to Pinboard on Android as my choice for Google Reader Sharing http://t.co/EupWQupM 3 months ago
  • FeedDemon+Custom Sharing XML->Pinboard; NewsRob + Save to Pinboard on Android - replacing Google Reader sharing, Reader now just for sync... 3 months ago
  • Doing a few Max - http://t.co/wL8dpWk4 - tutorials - what fun, first time in a decade I have played with this... 4 months ago

RSS pinboard -> links

RSS cmiles-consuming -> posts

  • Beats, Rhymes & Life: The Travels of a Tribe Called Quest 2012 January 30
    I have no idea when I first heard a Tribe Called Quest (official site) – but it was the early 2000s before I really ‘found’ them and started listening. I would not call myself a devotee – casual fan is probably the best description – but even as a casual fan I was excited when [...]
  • Hull Zero Three, Greg Bear 2012 January 15
    I am sad to say that the first time I saw Hull Zero Three by Greg Bear (official site) on Amazon I skipped over it because of the rating – in retrospect a somewhat sad reminder to myself about the value of ratings… Thankfully on the Potpourri of Science Fiction Literature blog I came across [...]
  • Happy New Year! End of 2011 Notes… 2012 January 1
    This blog made it thru 2011! A few notes about my media/reading that I thought might be fun if this blogs lasts a few years… -Reading: I am mostly reading on my Sprint Evo 4G Android Phone via the Kindle app. While the reading experience on such a small screen is unexciting the compelling feature [...]
  • Revelation Space Universe, Alastair Reynolds 2011 December 31
    I don’t have a good enough memory or record of what I was reading in the late 1990s or early 2000s to know if this is the truth – but the way I remember it is that after a lull where I had trouble finding any science fiction I was interested in reading I came [...]
  • Sea of Glass, Barry B. Longyear 2011 December 20
    I don’t remember seeing Sea of Glass, by Barry B. Longyear, on ‘top’ science fiction lists – or stumbling across it in website recommendations; but I do remember this novel from reading it in (about…) 1990. What I remember is the brutality, terror and a dystopian future world on the brink of war. The novel [...]
  • String Quartets 2 & 3, Kevin Volans, Balanescu Quartet, Kronos Quartet 2011 December 12
    I believe I first heard Kevin Volans‘s (homepage) String Quartet No. 2 – ‘Hunting: Gathering’ in the mid-1990s on a Kronos Quartet CD. While I can not say this was immediately one of my favorite pieces, I will say that sounds from and sections of the 2nd String Quartet have stayed with me – coming [...]
  • Norwegian Wood, Haruki Murakami 2011 December 6
    Norwegian Wood was not quite what I was expecting – the Murakami novels that I have read – A Wild Sheep Chase, Hard-Boiled Wonderland and the End of the World, Dance Dance Dance, The Wind-Up Bird Chronicle, Sputnik Sweetheart, Kafka on the Shore and After Dark – all seem to me to have some place [...]
  • Looking Glass, James R Strickland 2011 November 27
    Cyberpunk! James Strickland delivers the classic elements in Looking Glass – a future United States now carved into different countries, powerful corporations, cyber space, techy jargon, decks, jacking in and action! The strength of this novel is not in offering something insightful and new – but rather in being an intelligent and fascinating rec […]
  • Await Your Reply, Dan Chaon 2011 November 21
    Dan Chaon gives us a clever plot – with several converging stories – and disturbed characters that work together to create an enjoyable and slightly uncomfortable novel. There were sections that certainly made me think about my own life and identity, but overall I was not quite completely hooked/pulled in. Rating: 3 of 5 First [...]
  • Running the Sahara 2011 November 16
    Running the Sahara – directed by James Moll – follows three athletes as they run across the Sahara Desert. They run (and sometimes walk) thru Senegal, Mauritania, Mali, Niger, Libya and Egypt. The film does a great job of showing some of the amazing landscapes that they cross – with small glimpses of the people [...]
email: charles@cmiles.info

flickr -> pictures

1202 Group Picture 2 after the Colossal Cave Run

1202 Group Picture 1 after the Colossal Cave Race

1202 Charles And Joe After the Colossal Cave Run

1201 View from about 4 miles down the trail

1201 Water coming down Sycamore Dam

1201 Life of an Outdoor Footwear Buyer

1201 Blacketts Ridge Night Run, Dana near the top

1201 Arizona Trail in the Colossal Cave Area (Rincon Valley)

1201 Richard Coming Up to hill after the turn off the AZ Trail

1112 Sunset from Pontatoc Canyon

More Photos
Follow

Get every new post delivered to your Inbox.