Written by Allen Wyatt (last updated May 28, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021
John has a data entry worksheet that allows users to enter information. He would like to have a cell be automatically formatted to display exactly the number of decimal places that a user types. For example, if the user types "12.345" then he would like the cell to be automatically formatted to display 3 decimal places. John knows he could use the General format for a cell (which does this nicely), but that approach doesn't work if the user enters a value that ends in 0, such as "12.34500", which he would want formatted (automatically) to display 5 decimal places.
If you are thinking that you could use a custom format to address the need, that won't work. With every custom format we could come up with, Excel drops any trailing zeroes from what it displays. (Or, conversely, if the custom format includes "0" as a placeholder, it adds zeroes at the end of the entry.)
The easiest way to handle this, quite honestly, is to simply format the cells as Text before you start entering information. In that way, Excel will simply accept what is entered—including any trailing zeroes—and stuff it into the cell. You can, further, right-align the contents of the cells so that they at least look a bit more like numeric values.
The drawback to this is that you've got to be careful in using the values in formulas. The safest way is to simply surround any reference to the cell within the VALUE function, in this manner:
=VALUE(A1) * 1.375
Another approach is to create a macro that checks what is entered into a range of cells. Start by formatting the cells as Text, and then create a named range (DataEntry) from those cells. You can then add the following code to the code sheet for the worksheet you are using:
Private Sub Worksheet_Change(ByVal Target As Range) Dim c As Range Dim sEntry As String Dim dEntryNumber As Double Dim arr If Not Intersect(Target, Range("DataEntry")) Is Nothing Then Application.EnableEvents = False For Each c In Target.Cells If IsEmpty(c) Then c.NumberFormat = "@" ' Reset to Text format Else If IsNumeric(c) Then If Len(c.Value) = 0 Then c.NumberFormat = "@" ' Reset to Text format Else sEntry = c.Value dEntryNumber = CDbl(sEntry) arr = Split(sEntry, ".") If UBound(arr) = 1 Then ' Change NumberFormat in accordance with ' the number of digits after the decimal point c.NumberFormat = "0." & String(Len(arr(1)), "0") c.Value = dEntryNumber End If End If End If End If Next c Application.EnableEvents = True End If End Sub
The macro is triggered everytime something changes in the worksheet. It then checks to see if that change occurred in one of the cells in the DataEntry range. If so, then it examines what is entered in the cell (which Excel treats as text, since that's how the cell was formatted) and determines if it is a number and further how many digits there are to the right of the decimal place. It then formats the cell to have that many decimal places showing and stuffs the numeric value back into the cell.
The only condition where this approach won't work is if you place a value into a cell in the DataEntry range (which converts the cell to a numeric format) and then you enter a different numeric value in the same cell. The macro has no way of knowing, in that instance, if there are any trailing zeroes being entered. (Remember that trailing zeroes are only retained if the cell is formatted as Text. Since the cell is not, Excel lops off the trailing zeros and the macro works with that value as if it had been entered.)
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (1963) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
The ability to center text across a range of cells has long been a staple of experienced Excel users. Here's how to ...
Discover MoreThere are many ways that Excel allows you to highlight information in a cell. This tip examines a way to highlight values ...
Discover MoreIf you need to determine the font applied to a particular cell, you’ll need to use a macro. This tip presents several ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2022-05-28 12:43:25
Dave Bonin
There's a related issue which often comes up when using fractions such as halves, quarters, eighths, etc... , where you want to see the significant numbers but not a bunch of meaningless zeros.
Question: How do you hide trailing zeros and still get the decimal points to line up?
Answer: Use the "?" formatting character after the decimal point
To display a number to no more than three decimal places AND have the decimal points line up, use a number format like "#,##0.???"
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments