+ Reply to Thread
Results 1 to 7 of 7

Excel removing trailing zeros

  1. #1
    Registered User
    Join Date
    01-04-2013
    Location
    United Kingdom
    MS-Off Ver
    Office 2013
    Posts
    40

    Question Excel removing trailing zeros

    Hi there, I'm having trouble with data being entered into Excel. I have searched for answers on this, but nothing seems to match exactly what I need. At the moment I have a VBA program that reads data from an XML file (which gets spat out of a database program), and inserts this data into an Excel spreadsheet for further analysis. Much of the data comes down in the form of numbers to 1dp, but someone has come to me saying they need to know if someone entered "2" into the database program, or 2.0. When my code enters 2, or 2.0 into Excel, both end up being listed in the formula bar as just 2. Therefore I don't know if the user entered 2 or 2.0. Is there any way that you can alter the behaviour of Excel to stop removing trailing zeros, so that I can see the difference between a user entering 2 or 2.0? Now from what I've found through search, people just say set the cell format to text, but I have a whole load of functions that look to see if what's been entered is a number or not, and if I move over to a text based column I'll need to modify some complex logic tests that I really don't want to tinker with.

    As a side note, you might ask why does this matter, as 2 is the same as 2.0, but these numbers are assessment grades and it implies the user isn't grading properly, which needs to be flagged up.

    Hope that makes sense, thanks!

    Chris

  2. #2
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Excel removing trailing zeros

    Lets say the range where the entries will be made is A2:A23
    Please Login or Register  to view this content.
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  3. #3
    Forum Expert ben_hensel's Avatar
    Join Date
    01-26-2012
    Location
    Northeast USA
    MS-Off Ver
    Office 365
    Posts
    2,043

    Re: Excel removing trailing zeros

    2 and 2.0 are exactly the same number. Excel cannot tell the difference. So if it is given 2 and 2.0 as number inputs, it will just apply whatever the formatting for the cell is -- whether that's the "General" default of only showing precision if it matters, or "Number" format with the same number of decimals in every cell, or whatever.

    That's why they must be entered as text -- because "2" and "2.0" are different text strings.

    The way I would approach this in your shoes would be to split 'em into two columns.

    Bring the data in as a column of text. That will allow you to check whether they are being entered correctly.

    And then insert a helper column that's directly to the right of it, and have the helper column force it to a number.

    A1 = "2.0"
    // The text pulled in
    B1 = VALUE(A1)
    // the text being forced to assess as a number. You can also put all of your "text testing" in here as well if you want -- that might save downstream?

    Then run your math off the new helper column, rather than the column of data coming in.

    It's a bit cludgy, but I think this is probably the best way to have your cake and eat it -- well, or you could go and nest VALUE() around every reference of the data coming in as text, but that might be a bit of a hassle.

    But if you need to check if it's entered correctly AND you need to be able to distinguish between numbers and text, than I think putting in a helper column is probably the way to go.
    Click the [* Add Reputation] Button to thank people for help!
    Post Attachment: Click [Go Advanced] under the reply box; then click [Manage Attachments] to launch the attachments dialogue.
    Mark Thread Solved: Go to the top of the first post; Select [Thread Tools] dropdown; Pick the [Mark thread as Solved] option.

  4. #4
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Excel removing trailing zeros

    Something to try

    BEFORE running VBA, format the column(s) accepting these values to TEXT
    run the VBA

    Does the column contain both 2's and 2.0's?
    If it does then we probably can add something at the end of the VBA to give you what you require
    (ie format each cell as a number, but to either 0 or 1 decimal place based on original input - leaving values as numbers and structure of worksheet unchanged)

    Let me know
    Last edited by kev_; 02-08-2018 at 02:34 PM.
    Click *Add Reputation to thank those who helped you. Ask if anything is not clear

  5. #5
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Re: Excel removing trailing zeros

    or set range to number or set different for individual cells

    number.jpg
    Last edited by sandy666; 02-08-2018 at 02:35 PM.

  6. #6
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,819

    Re: Excel removing trailing zeros

    Can we assume that VBA is seeing the entries as text, so that it can distinguish between 2 and 2.0? Assuming that is true, then I would be inclined to have VBA check for 2 or 2.0, store a new "flag", then have VBA write the value to the current cell, and either add a comment to the cell or change the formatting or something like that to flag the "may not have been graded properly". That should allow you to avoid the "enter as text" option which sounds like it will significantly interfere with the current structure of the spreadsheet, but still allow you to flag these values for evaluation.
    Please Login or Register  to view this content.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  7. #7
    Registered User
    Join Date
    01-04-2013
    Location
    United Kingdom
    MS-Off Ver
    Office 2013
    Posts
    40

    Re: Excel removing trailing zeros

    Thanks for all your responses! Interesting discussion. ben_hensel's approach is basically what I am doing. So I bring down the "raw data" into a column, I then work off that raw data to put it into a format that my analysis needs, and this is the so called helper column. It's this function that I'm reluctant to tinker with, because the logic in this helper column is quite complex because it's having to test for lots of different types of input. I think going down the road of having the raw data set as text is the only viable way forward though. In my helper column where I'm testing if it's a number I can just stick VALUE() inside the ISNUMBER() test, and that should solve the problem. I just need to get my hands dirty in the formula.

    I can't really do much inside VBA, my code does just work with the data as text and it's only when it gets added into Excel does it lose the ".0". But my problem is that there is an awful lot of data being read from the XML file, and entered into the spreadsheet, so much so that going back and forth from the sheet was having a huge impact on run time. So I now just build up a 2d array of the data I want to add to the sheet (because this is done over many hundreds of columns and hundreds of rows), and then just write that array in one go back to the sheet. By that virtue I don't really want to be working directly with cells, as that would slow the code down, and when the data gets updated I'd have to then find a way to undo the changes that were made, because new/updated data then comes down.

    Thanks for your input though!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Keep trailing zeros, but only if they're there...
    By rbrian in forum Excel - New Users/Basics
    Replies: 5
    Last Post: 12-02-2013, 10:47 AM
  2. Delete Trailing Spaces without Removing leading zeros WITHOUT TRIM
    By Zach51215 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-20-2013, 04:24 PM
  3. Excel doesnt save currency with trailing Zeros
    By cmallbones in forum Excel General
    Replies: 18
    Last Post: 05-13-2013, 11:04 AM
  4. [SOLVED] Add Trailing Zeros
    By sweetkel23 in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 12-07-2012, 01:42 AM
  5. [SOLVED] Delete trailing zeros.
    By krasher in forum Excel - New Users/Basics
    Replies: 8
    Last Post: 09-21-2012, 10:43 AM
  6. Replies: 5
    Last Post: 04-09-2012, 02:28 PM
  7. Leading and Trailing Zeros
    By ctang in forum Excel General
    Replies: 1
    Last Post: 12-19-2011, 10:18 AM
  8. trailing zeros
    By htorres in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 06-23-2007, 03:00 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1